// Get base url
url = document.location.href;
xend = url.lastIndexOf("/") + 1;
var base_url = url.substring(0, xend);

//function used to validate the edit member details
function funEditMemberDetails(formname)
{
  with(document.forms[formname])
  {
	if(trimAll(txt_loginName.value)=="")
	{
	  alert("Please enter your username");
	  txt_loginName.focus();
	}
	else if(trimAll(txt_email.value)!="" && !(validateEmail(txt_email.value)))
	{
	  alert("Please enter valid email address");
	  txt_email.focus(); 
	}
	/*else if(trimAll(txt_City.value)=="")
	{
	  alert("Please enter city");
	  txt_City.focus();
	}
	else if(trimAll(drpdwn_state.value)=="")
	{
	  alert("Please select state");
	  drpdwn_state.focus();				
	}
	else if(trimAll(txt_zip.value)=="")
	{
	  alert("Please enter zipcode");
	  txt_zip.focus();
	}
	else if(trimAll(txt_County.value)=="")
	{
	  alert("Please enter county");
	  txt_County.focus();
	}
	else if(trimAll(txt_refnumber.value)=="")
	{
	  alert("Please enter referral number");
	  txt_refnumber.focus();
	}*/
	else
	{
	  hdn_mode.value="edit";
	  submit();
	}

  }//with ends

}

//function used to validate the registration details
function funMemberRegistration(strBtnType)
{
  with(document.forms["frm_memberReg"])
  {
	    document.getElementById("td_reg_mem_successmsg").innerHTML="";
	    var strBox1_DisplatStatus = document.getElementById("tr_reg_mem_box1").style.display;
		var strBox2_DisplatStatus = document.getElementById("tr_reg_mem_box2").style.display;
		var strBox3_DisplatStatus = document.getElementById("tr_reg_mem_box3").style.display;
		if(strBtnType=="go")
		{
           if(strBox1_DisplatStatus!="none")
		   {
				
				if(trimAll(txt_reg_mem_email.value)!="" && !(validateEmail(txt_reg_mem_email.value)))
				{
					alert("Please enter valid email address");
					txt_reg_mem_email.focus(); 
				}
				/*else if(trimAll(txt_reg_mem_city.value)=="")
				{
				  alert("Please enter city");
				  txt_reg_mem_city.focus();
				}
				else if(trimAll(txt_reg_mem_state.value)=="")
				{
				  alert("Please select state");
				  txt_reg_mem_state.focus();				
				}
				else if(trimAll(txt_reg_mem_zip.value)=="")
				{
				  alert("Please enter zipcode");
				  txt_reg_mem_zip.focus();
				}
				else if(trimAll(txt_reg_mem_county.value)=="")
				{
				  alert("Please enter county");
				  txt_reg_mem_county.focus();
				}
				else if(trimAll(txt_reg_mem_refnumber.value)=="")
				{
				  alert("Please enter referral number");
				  txt_reg_mem_refnumber.focus();
				}*/
				else if(trimAll(txt_reg_mem_loginName.value)=="")
				{
				  alert("Please enter your username");
				  txt_reg_mem_loginName.focus();
				}
				else if(validateEmail(txt_reg_mem_loginName.value))
				{
					alert("Please enter valid username");
					txt_reg_mem_loginName.focus(); 
				}
				else if(trimAll(txt_reg_mem_password.value)=="")
				{
				  alert("Please enter your password");
				  txt_reg_mem_password.focus();
				}
				else if(trimAll(txt_reg_mem_confirmpassword.value)=="")
				{
				  alert("Please confirm your password");
				  txt_reg_mem_confirmpassword.focus();
				}
				else if(trimAll(txt_reg_mem_confirmpassword.value)!=trimAll(txt_reg_mem_password.value))
				{
				  alert("Password and confirm password not matched.");
				  txt_reg_mem_password.focus();
				}
			    else
			    {
			     //function used to verify the account details
                 funAjaxVerifyMember();
			    }
		   }
		  else if(strBox2_DisplatStatus!="none")
		  {
            //hide step1 and step2
	        document.getElementById("tr_reg_mem_box1").style.display="none";
		    document.getElementById("tr_reg_mem_box2").style.display="none";
		   
		    //display step3
			document.getElementById("tr_reg_mem_box3").style.display="";
			document.getElementById("lbl_reg_mem_heading").innerHTML="Page 3 of 3";
            
			//display back button
			document.getElementById("img_reg_mem_back").style.display="";
		  }
		  else
		  {
            hdn_mode.value="register";  
			submit();
		  }
	   }
	   else
         {
		    //hide back button
			document.getElementById("img_reg_mem_back").style.display="none";

		    //hide step2 and step3
	        document.getElementById("tr_reg_mem_box2").style.display="none";
		    document.getElementById("tr_reg_mem_box3").style.display="none";
		   
		    //display step1
			document.getElementById("tr_reg_mem_box1").style.display="";
			document.getElementById("lbl_reg_mem_heading").innerHTML="Page 1 of 3";
		}

  }//with ends
}

//function used to verify the account details
function funAjaxVerifyMember()
{
  with(document.forms["frm_memberReg"])
  {
    var strUserName=trimAll(txt_reg_mem_loginName.value);
	var strPostRegData="usrname="+strUserName;

	//show loading image 
	document.getElementById("tr_reg_mem_loading").style.display="";
    
	 url="verify_memberaccount.php";

	//Does URL begin with http?
	if(url.substring(0, 4) != 'http')
	{
	  url = base_url + url;
	}
	
	http_req = null;
	if(window.XMLHttpRequest)     http_req = new XMLHttpRequest();
	else if(window.ActiveXObject) http_req = new ActiveXObject("Microsoft.XMLHTTP");
	if(http_req)
	{
		http_req.onreadystatechange = funResponsVerifyUserAccount;
		http_req.open("POST", url, true);
		http_req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http_req.send(strPostRegData);
	}
  }
}

//function used to verify the account details
function funResponsVerifyUserAccount()
{
  if(http_req.readyState== 4)
  {
	//show loading image 
	document.getElementById("tr_reg_mem_loading").style.display="none";

    //Response text is=> "valid" or "invalid"
	var strResponseTxt= http_req.responseText;	

	var strClsName;
    with(document.forms["frm_memberReg"])
	{
		 if(strResponseTxt=="valid")
		 {		 	
			 //hide back button
			document.getElementById("img_reg_mem_back").style.display="";

		    //hide step1 and step3
	        document.getElementById("tr_reg_mem_box1").style.display="none";
		    document.getElementById("tr_reg_mem_box3").style.display="none";
		   
		   //Getting enetered values from step1 controls and assing it into the <td> tag
		    var ArrStep2Values=new Array(["td_reg_mem_email",""],["td_reg_mem_city",""],["td_reg_mem_state",""],["td_reg_mem_Zip",""],["td_reg_mem_County",""],["td_reg_mem_RefNumber",""],["td_reg_mem_Username",""],["td_reg_mem_Password",""]);
            
			ArrStep2Values[0][1]=trimAll(txt_reg_mem_email.value);
			ArrStep2Values[1][1]=trimAll(txt_reg_mem_city.value);
			ArrStep2Values[2][1]=trimAll(drpdwn_reg_mem_state.options[drpdwn_reg_mem_state.selectedIndex].text);
			ArrStep2Values[3][1]=trimAll(txt_reg_mem_zip.value);
			ArrStep2Values[4][1]=trimAll(txt_reg_mem_county.value);
			ArrStep2Values[5][1]=trimAll(txt_reg_mem_refnumber.value);
			ArrStep2Values[6][1]=trimAll(txt_reg_mem_loginName.value);
			ArrStep2Values[7][1]=trimAll(txt_reg_mem_password.value);

			for(ctrl=0;ctrl<ArrStep2Values.length;ctrl++)
			{
			 document.getElementById(ArrStep2Values[ctrl][0]).innerHTML="&nbsp;"+ArrStep2Values[ctrl][1];
			}

		    //display step2
			document.getElementById("tr_reg_mem_box2").style.display="";
			document.getElementById("lbl_reg_mem_heading").innerHTML="Page 2 of 3";
		 }
		 else
		 {
		   document.getElementById("td_reg_mem_successmsg").className="error_message";
		   document.getElementById("td_reg_mem_successmsg").innerHTML="Username already exist.";
		   document.getElementById("txt_reg_mem_loginName").focus();
		 }

	}//with ends
  
  }//response check ends
}

//function used to validate the add new advertisement form controls
function funAds()
{
  with(document.forms["frm_ads"])
  {
	 if(trimAll(txt_adKeyWords.value)=="")
	{
	 alert("Please enter keywords");
	 txt_adKeyWords.focus();
	 return false;
	}
	else if(trimAll(txt_adFromDate.value)=="" && trimAll(txt_adToDate.value)=="")
	{
		 alert("Please enter the date in the format of mm/dd/yyyy");
		 txt_adFromDate.focus();
		 return false;
	}
	else if(trimAll(txt_adFromDate.value)=="")
	{
		 alert("Please enter the from date in the format of mm/dd/yyyy");
		 txt_adFromDate.focus();
		 return false;
	}
	else if(trimAll(txt_adToDate.value)=="")
	{
		 alert("Please enter the end date in the format of mm/dd/yyyy");
		 txt_adToDate.focus();
		 return false;
	}


	if(trimAll(txt_adFromDate.value)!="" && funIsValidDate(trimAll(txt_adFromDate.value))==false)
	{
	 alert("Please enter the date in the format of mm/dd/yyyy");
	 txt_adFromDate.focus();
	 return false;
	}
	if(trimAll(txt_adToDate.value)!="" && funIsValidDate(trimAll(txt_adToDate.value))==false)
	{
	 alert("Please enter the date in the format of mm/dd/yyyy");
	 txt_adToDate.focus();
	 return false;
	}
	else
	 {	
		//To check the banner type
		strBannerVal=trimAll(txt_adBanner.value);
		if(strBannerVal!="")
		{				 
		   Extension=funGetFileExtension(strBannerVal);
		   if(Extension=="swf" || Extension=="jpeg" || Extension=="jpg" || Extension=="gif" || Extension=="png")
		   {
			 //allow to add
		   }
		   else
		   {
			alert("Please select banner file of type swf, jpg, jpeg , png or gif");
			txt_adBanner.focus();
			return false;
		   }
		}

		//To check the image type
		var ImgCount=parseInt(hdn_imgcount.value);
		SelectedImgCount=0;
		for(img=1;img<=ImgCount;img++)
		{
			strImgCtrlId="txt_adImage"+img;		
			imgvalue=trimAll(elements[strImgCtrlId].value);
			if(imgvalue=="")
			 continue;
			else
			{				 
			   Extension=funGetFileExtension(imgvalue);
			   Extension=Extension.toLowerCase();
			   if(Extension=="jpeg" || Extension=="jpg" || Extension=="gif" || Extension=="png" || Extension=="pdf" || Extension=="swf" )
			   {
				 SelectedImgCount++;
			   }
			   else
			   {
				alert("Please select image file of type jpg, png, jpeg, pdf or gif");
				elements[strImgCtrlId].focus();
				return false;
			   }
			}
		}//for loop of image ends

		hdn_mode.value="save";  
		submit();
		return true;
	 }
	
  }//with ends
}

//function used to getting list of dates between the given two dates
function funDisplayAvlblDate()
{
	with(document.forms["frm_ads"])
	{
		document.getElementById("td_AvlblDateDisplay").innerHTML = "";

		var FromDate =trimAll(txt_adFromDate.value)
		var ToDate   = trimAll(txt_adToDate.value)

		FromDate = FromDate.replace(/-/g,"/");
		ToDate = ToDate.replace(/-/g,"/");

		if(FromDate=="" && ToDate=="")
		{
		 alert("Please enter the date in the format of mm/dd/yyyy");
		 txt_adFromDate.focus();
		}
		else if(FromDate=="")
		{
		 alert("Please enter the start date in the format of mm/dd/yyyy");
		 txt_adFromDate.focus();
		}
		else if(ToDate=="")
		{
		 alert("Please enter the end date in the format of mm/dd/yyyy");
		 txt_adToDate.focus();
		}


		if(FromDate!="" && funIsValidDate(FromDate)==false)
		{
		 alert("Please enter the date in the format of mm/dd/yyyy");
		 txt_adFromDate.focus();
		}
		if(ToDate!="" && funIsValidDate(ToDate)==false)
		{
		 alert("Please enter the date in the format of mm/dd/yyyy");
		 txt_adToDate.focus();
		}

		if(FromDate!="" && ToDate!="")
		{
			var date1 = new Date(FromDate); 
			var date2 = new Date(ToDate); 
			var sec = date2.getTime() - date1.getTime(); 
			dayDif = sec/(3600000*24);
			if(dayDif < 0)
			{
			  alert("Please enter correct dates for from date and to date");
			  txt_adFromDate.focus();
			  return false;
			}
			else
			{
			 //document.getElementById("td_AvlblDateDisplay").innerHTML=postbackresponse;
				 poststr = "FromDate="+FromDate+"&ToDate="+ToDate;
				ajaxpack.postAjaxRequest("dateCheck.php", poststr, Process_datecheck, "txt");
			}

	    }//if(FromDate!="" && ToDate!="") part ends

   }//with ends
}

//function used to display the comfirm dates starts
function Process_datecheck()
{
	var myajax=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax.readyState == 4){ //if request of file completed
	if (myajax.status==200 || window.location.href.indexOf("http")==-1)
	{ //if request was successful or running script locally
		if (myfiletype=="txt")
		{
			var postbackresponse=myajax.responseText;
			if (document.getElementById("td_AvlblDateDisplay").innerHTML != "")
			{
				document.getElementById("td_AvlblDateDisplay").innerHTML = "";
			}
			document.getElementById("td_AvlblDateDisplay").innerHTML=postbackresponse;
		}
		else
		alert(myajax.responseXML)
	}
	}
}

//function used to validate the edit advertisement form controls
function funEditAds()
{
  with(document.forms["frm_ads"])
  {
	 if(trimAll(txt_adKeyWords.value)=="")
	{
	 alert("Please enter keywords");
	 txt_adKeyWords.focus();
	}
	else if(trimAll(txt_adFromDate.value)=="" && trimAll(txt_adToDate.value)=="")
	{
	 alert("Please enter the date in the format of mm/dd/yyyy");
	 txt_adFromDate.focus();
	}
	else if(trimAll(txt_adFromDate.value)=="")
	{
	 alert("Please enter the from date in the format of mm/dd/yyyy");
	 txt_adFromDate.focus();
	}
	else if(trimAll(txt_adToDate.value)=="")
	{
	 alert("Please enter the end date in the format of mm/dd/yyyy");
	 txt_adToDate.focus();
	}
	else if(trimAll(txt_adFromDate.value)!="" && funIsValidDate(trimAll(txt_adFromDate.value))==false)
	{
	 alert("Please enter the date in the format of mm/dd/yyyy");
	 txt_adFromDate.focus();
	}
	else if(trimAll(txt_adToDate.value)!="" && funIsValidDate(trimAll(txt_adToDate.value))==false)
	{
	 alert("Please enter the date in the format of mm/dd/yyyy");
	 txt_adToDate.focus();
	}
	else
	 {
   	    hdn_mode.value="save";  
		submit();
		return true;
	 }
	
  }//with ends
}

//function used to validate the add ad's image form
//strProcessType may be ==> addimage - set_main_image - delete
function funManage_AdImage(strProcessType,strImgId)
{
  with(document.forms["frm_EditAdsImage"])
  {
	  if(strProcessType=="addimage")
	  {
		 var intAllowedImageCount=parseInt(hdn_AllowedImageCount.value);
		 var intUploadedImageCount=parseInt(hdn_UploadedImageCount.value);
         intUploadedImageCount=intUploadedImageCount+1;

		 if(intUploadedImageCount>intAllowedImageCount)
		 {
		    alert("You can upload "+intAllowedImageCount+" images only.");
			return false;		
		 }
		 else
		  {
			//To check the banner type
			strImageVal=trimAll(txt_image.value);
			if(strImageVal=="")
			{
			  alert("Please select image file of type jpg, jpeg, png or gif");
			  txt_image.focus();
			  return false;		
			}
			else
			{				 
			   Extension=funGetFileExtension(strImageVal);
			   Extension=Extension.toLowerCase();
			   if(Extension=="jpeg" || Extension=="jpg" || Extension=="gif" || Extension=="png")
			   {
				 //allow to add
			   }
			   else
			   {
				alert("Please select image file of type jpg , jpeg, png or gif");
				txt_image.focus();
				return false;
			   }
			}
		  }
	  }
	  else if(strProcessType=="delete_image")
	  {
	     if(!confirm("Are you sure want to delete this image?"))
		 {
          return false;
		 }
	  }

	  hdn_mode.value=strProcessType;
	  hdn_ImageId.value=strImgId;

	  submit();
	  return true;
	  
  }//with ends
}

//function used to validate the distribute ad's form
function funDistributeAds(strStep)
{
 var formname="frm_DistributeAds";
  with(document.forms[formname])
  {
	 if(strStep=="step3")
	 {
       var strNumofMembrChkBoxes=hdn_Step2_MemberCount.value;
	   if(funFindCheckedChkbox(formname,"member_chkbx_",strNumofMembrChkBoxes)==false)
	   {
	     alert("Please select a member for your promotion");
		 return false
	   }
	 }

	 hdn_step.value=strStep;
	 submit();
	 return true;
	  
  }//with ends
}
