
requestFBExtendedPermissions = function(context)
{
	var permissionsAndRedir = $.ajax({ type: "GET", url: "/users/extendedperm/" + context, async: false }).responseText;
	var fields = permissionsAndRedir.split("*");
	var permissions = fields[0];
	var redir = fields[1];
	
	if (permissions.length != 0) {
		FB.Connect.showPermissionDialog(permissions, function(perms) {if (redir.length > 0) window.location = redir});
	}
	else {
		if (redir.length > 0) window.location = redir;
	}
}

buttonRequestFBExtendedPermissions = function ()
{
	requestFBExtendedPermissions("button");
}

pageRequestFBExtendedPermissions = function (uid)
{
	requestFBExtendedPermissions("page");
}

linkRequestFBExtendedPermissions = function ()
{
	requestFBExtendedPermissions("link");
}

function bonusLogout() {
	window.location = '/users/logout';
}

function bonusFBStreamPublishCallback(entityid, requestid, postid, exception, data) {
	if (postid) {
		//alert(entityid);
		$.post('/users/recordsocialevent', { entityid: entityid, requestid: requestid, type: 'share', media: 'FB', postid: postid } );
	}
}

function bonusFBLikeCallback(url, requestid) {
	//alert(requestid);
	$.post('/users/recordsocialevent', { type: 'like', requestid: requestid, media: 'FB', url: url } );
}

function updateCompareText() {
	var counter = 1;
	var namesIndex = 0;
	if (document.CompareProductsForm != undefined && document.CompareProductsForm != null) {
		document.getElementById("compare1").innerText="> Compare up to 3 items";
		document.getElementById("compare2").innerText="";
		document.getElementById("compare3").innerText="";
		document.getElementById("compare1").textContent="> Compare up to 3 items";
		document.getElementById("compare2").textContent="";
		document.getElementById("compare3").textContent="";
		document.getElementById("compare2").style.display="none";
		document.getElementById("compare3").style.display="none";
		for ( var j = 0; j < document.CompareProductsForm.length; j++) {
			currentElement = document.CompareProductsForm.elements[j];
			if (currentElement.type == 'checkbox') {
				if (currentElement.checked == true) {
					document.getElementById("compare"+counter).innerText = counter+". "+document.getElementById("compareName["+namesIndex+"]").value;
					document.getElementById("compare"+counter).textContent  = counter+". "+document.getElementById("compareName["+namesIndex+"]").value;
					document.getElementById("compare"+counter).style.display="block";
					document.getElementById("div"+namesIndex).style.backgroundColor="#ECF4FF";
					counter++;
				}
				else document.getElementById("div"+namesIndex).style.backgroundColor="#fff";
				namesIndex++;
			}
		}
		if (counter==1){
		document.getElementById("compare").style.display="none";	
		}else document.getElementById("compare").style.display="block";
	}
}
//Shows or hides a block passed in by Id
function show_hide(divId) {
	var item=document.getElementById(divId);
	if (item.style.display!='block'){
		document.getElementById(divId).style.display='block';
	}else document.getElementById(divId).style.display='none';
}
function show_deal(divIdNum,dealCount) {
	for (i=1;(i<=dealCount)&&(i<=4);i++){
		document.getElementById('position'+i).style.display='none';
		document.getElementById('positionImg'+i).style.display='none';
		document.getElementById('positionButton'+i).style.display='none';
		document.getElementById('tab'+i).className='';
	}
	for (i=1;i<=4;i++){
	document.getElementById('shareThis'+i).style.display='none';
	}
	
	document.getElementById('position'+divIdNum).style.display='block';
	document.getElementById('positionImg'+divIdNum).style.display='block';
	document.getElementById('positionButton'+divIdNum).style.display='block';
	document.getElementById('shareThis'+divIdNum).style.display='block';
	document.getElementById('tab'+divIdNum).className='active';
}
//Replaces broken images with branded noImage graphic--works only in IE
function onImgErrorSmall(source)
{
source.src = "/images/noImage.jpg";
// disable onerror to prevent endless loop
source.onerror = "";
return true;
}

function onImgErrorLarge(source)
{
source.src = "/images/noImageLarge.jpg";
// disable onerror to prevent endless loop
source.onerror = "";
return true;
}

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}
function ValidateAutoSearch(form){
	
	if ((form.what.value==null)||(form.what.value=="")||(form.what.value=="make and/or model")){
		alert("Please enter at least one search term, such as 'Ford' or 'Escalade'.");
		form.what.focus();
		return false;
	}
	if ((form.where.value==null)||(form.where.value=="")||(form.where.value=="your ZIP code")||(form.where.value=="Your 5-digit Zip Code")){
		alert("Please enter your 5-digit ZIP code (e.g. '19057').");
		form.where.focus();
		return false;
	}
}

function ValidateAutoSubmit(form){
	
	if ((form.UsedAutoShopperEmail.value==null)||(form.UsedAutoShopperEmail.value=="")||(form.UsedAutoShopperEmail.value=="your email address")){
		alert("Please enter your Email");
		form.UsedAutoShopperEmail.focus();
		return false;
	}
	if (echeck(form.UsedAutoShopperEmail.value)==false){
		alert("The e-mail address you entered is not valid. Please enter your valid email address to continue.");
		form.UsedAutoShopperEmail.value="";
		form.UsedAutoShopperEmail.focus();
		return false;
	}
	if ((form.UsedAutoShopperFirstname.value==null)||(form.UsedAutoShopperFirstname.value=="")||(form.UsedAutoShopperFirstname.value=="first name")){
		alert("Please enter your First Name");
		form.UsedAutoShopperFirstname.focus();
		return false;
	}
	if ((form.UsedAutoShopperLastname.value==null)||(form.UsedAutoShopperLastname.value=="")||(form.UsedAutoShopperLastname.value=="last name")){
		alert("Please enter your Last Name");
		form.UsedAutoShopperLastname.focus();
		return false;
	}
	if ((form.UsedAutoShopperZipcode.value==null)||(form.UsedAutoShopperZipcode.value=="")||(form.UsedAutoShopperZipcode.value=="zipcode")){
		alert("Please enter your Zip Code");
		form.UsedAutoShopperZipcode.focus();
		return false;
	}
	if (stripAlphaChars(form.UsedAutoShopperZipcode.value).length != 5){
		alert("Please enter your complete 5-digit Zip Code.");
		form.UsedAutoShopperZipcode.focus();
		return false;
	}
	form.UsedAutoShopperZipcode.value=stripAlphaChars(form.UsedAutoShopperZipcode.value);

	if ((form.UsedAutoShopperPhoneHomeAreacode.value==null)||(form.UsedAutoShopperPhoneHomeAreacode.value=="")||(form.UsedAutoShopperPhoneHomeAreacode.value=="areacode")){
		alert("Please enter your areacode");
		form.UsedAutoShopperPhoneHomeAreacode.focus();
		return false;
	}
	if (stripAlphaChars(form.UsedAutoShopperPhoneHomeAreacode.value).length != 3){
		alert("Please enter your complete areacode.");
		form.UsedAutoShopperPhoneHomeAreacode.focus();
		return false;
	}
	form.UsedAutoShopperPhoneHomeAreacode.value=stripAlphaChars(form.UsedAutoShopperPhoneHomeAreacode.value);

	if ((form.UsedAutoShopperPhoneHomePrefix.value==null)||(form.UsedAutoShopperPhoneHomePrefix.value=="")||(form.UsedAutoShopperPhoneHomePrefix.value=="prefix")){
		alert("Please enter your prefix");
		form.UsedAutoShopperPhoneHomePrefix.focus();
		return false;
	}
	if (stripAlphaChars(form.UsedAutoShopperPhoneHomePrefix.value).length != 3){
		alert("Please enter your complete prefix.");
		form.UsedAutoShopperPhoneHomePrefix.focus();
		return false;
	}
	form.UsedAutoShopperPhoneHomePrefix.value=stripAlphaChars(form.UsedAutoShopperPhoneHomePrefix.value);

	if ((form.UsedAutoShopperPhoneHomeSuffix.value==null)||(form.UsedAutoShopperPhoneHomeSuffix.value=="")||(form.UsedAutoShopperPhoneHomeSuffix.value=="suffix")){
		alert("Please enter your suffix");
		form.UsedAutoShopperPhoneHomeSuffix.focus();
		return false;
	}
	if (stripAlphaChars(form.UsedAutoShopperPhoneHomeSuffix.value).length != 4){
		alert("Please enter your complete suffix.");
		form.UsedAutoShopperPhoneHomeSuffix.focus();
		return false;
	}
	form.UsedAutoShopperPhoneHomeSuffix.value=stripAlphaChars(form.UsedAutoShopperPhoneHomeSuffix.value);
	
	return true;
 }
function stripAlphaChars(pstrSource) 
{ 
var m_strOut = new String(pstrSource); 
    m_strOut = m_strOut.replace(/[^0-9]/g, ''); 

    return m_strOut; 
}

var allHTMLTags = new Array();

function sameHeight(theClass,theNumber,extra) {
maxHeight=0;
//Create Array of All HTML Tags
//Loop through all tags using a for loop
for (i=0; i<theNumber; i++) {
//get height
if (document.getElementById(theClass+i).offsetHeight > maxHeight){
	maxHeight=document.getElementById(theClass+i).offsetHeight;
}
}
for (i=0; i<theNumber; i++) {
	//Get all tags with the specified class name.
	document.getElementById(theClass+i).style.height=maxHeight+extra+'px';
}
}
//---------------------------------------------Pop-up Div Utilities
//-------------------Show and Hide Coupon overlay--------------
function showCoupons(divNum){
	var divName='coupons'+divNum;
	var overlayName='overlay'+divNum;
	document.getElementById(divName).style.display="block";
	document.getElementById(overlayName).style.display="block";
}
function hideCoupons(divNum){
	var divName='coupons'+divNum;
	var overlayName='overlay'+divNum;
	document.getElementById(divName).style.display="none";
	document.getElementById(overlayName).style.display="none";
}
//-------------------Show and Hide Lg Image overlay--------------
function showZoomImg(div){
	var divName=div;
	var overlayName='overlay'+div;
	document.getElementById(divName).style.display="block";
	document.getElementById(overlayName).style.display="block";
}
function hideZoomImg(div){
	var divName=div;
	var overlayName='overlay'+div;
	document.getElementById(divName).style.display="none";
	document.getElementById(overlayName).style.display="none";
}
//------------------------------Save/Manage/Delete Coupons--------------------
function saveCoupon(couponId){
	if (getCouponNum()==0){
		Set_Cookie('couponSave', couponId, 30, '/', '', '');
		updateSavedCoupons();
	}else if (getCouponNum()<10){
		if (!findDuplicates(couponId)){
		Set_Cookie('couponSave', Get_Cookie('couponSave')+','+couponId, 30, '/', '', '');
		updateSavedCoupons();
		}else alert('That coupon has already been saved.');
	}else alert('At this time, you can only save 10 coupons.\n Please delete some coupons before you save more.');
}
function updateSavedCoupons(){
	if (document.getElementById("couponsSavedWrapper")){
	document.getElementById("couponsSavedWrapper").innerHTML = '<a href="/coupons/saved/'+getCouponQuery()+'" class="couponsSaved"><img src="/images/couponsSaved.png"></img> You have '+getCouponNum()+' Saved Coupons</a>';
	document.getElementById("couponsSavedWrapper").HTMLContent  = '<a href="/coupons/saved/'+getCouponQuery()+'" class="couponsSaved"><img src="/images/couponsSaved.png"></img> You have '+getCouponNum()+' Saved Coupons</a>';
	}
}
function getCouponNum(){
	if (Get_Cookie('couponSave')){
		var temp = new Array();
		temp = Get_Cookie('couponSave').split(',');
		return temp.length;
	}else return 0;
}
function getCouponQuery(){
	if (Get_Cookie('couponSave')){
	return Get_Cookie('couponSave');
	}else return 0;
}
function findDuplicates(couponId){
	if (Get_Cookie('couponSave').search(couponId)!=-1){
		return true;
	}else return false;
}
function deleteCoupon(couponId){
	if (findDuplicates(couponId)){
		if (getCouponNum()==1){
			Set_Cookie('couponSave', '', 1, '/', '', '');
			window.location = '/coupons/saved/'+getCouponQuery();
		}else{
			var cookie='';
			if (findDuplicates(couponId+',')){
				cookie=Get_Cookie('couponSave').replace(couponId+',','');
			}else cookie=Get_Cookie('couponSave').replace(','+couponId,'');
			Set_Cookie('couponSave', cookie, 30, '/', '', '');
			window.location = '/coupons/saved/'+getCouponQuery();
		}
	}else alert('That coupon has already been deleted.');
}
function updatePage(){
	updateCompareText();
	updateSavedCoupons();	
}
function wtpopup(url){
    window.open(url, 'wtpopup', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=250');
  }
function popitup(url) {
	newwindow=window.open(url,'','height=600,width=400');
	if (window.focus) newwindow.focus();
	return false;
}
function sweepsWelcome(){
	document.getElementById('sweepsWelcome').style.display="block";
	document.getElementById('sweepsOverlay').style.display="block";
}
function hideSweepsWelcome(){
	document.getElementById('sweepsWelcome').style.display="none";
	document.getElementById('sweepsOverlay').style.display="none";
}
