function addAddressControl(formname,from) 
{
	with(document.forms[formname])
	{
		MaxLimit = parseFloat(hdn_MaxmiumAddress.value);
		if(from=="register")
		{
			currentLimit=parseFloat(hdn_AddressCtrlCount.value);
		}
		else if(from=="edit")
		{
			totalAddressCount = parseFloat(hdn_totalAddressCtrlCount.value);
			if(totalAddressCount>0)
			{
				addressctrlvalue=hdn_AddressCtrlCount.value;
				if(addressctrlvalue==1)
					currentLimit=parseFloat(hdn_totalAddressCtrlCount.value);
				else
					currentLimit=parseFloat(hdn_AddressCtrlCount.value);
			}
			else
			{
				currentLimit=parseFloat(hdn_AddressCtrlCount.value);
			}
		}
		num=parseInt(currentLimit)+1;
		if(num<=MaxLimit)
		{
			document.getElementById("browse_tr"+num).style.display="";
			hdn_AddressCtrlCount.value=num;
		} 
	}
}

//verification number
function fnShowCvv() {
	filename  = "cvv_help.php";
	features = "top=40,left=210,width=400,height=400,resizable=no,scrollbars=yes";
	windowName = 'formTarget' + (new Date().getTime());
	open (filename , windowName, features);
}

function fnShowCCFrm(From,formname)
{
	if(From == 1){
		document.getElementById("trCreditCard").style.display=""
	}
	else
	{
		document.getElementById("trCreditCard").style.display="none"
		with(document.forms[formname])
		{
			for(Cindex=0;Cindex<4;Cindex++)
			{
				rd_CrdtCardType[Cindex].checked=false;
			}
			txtbx_CrdtCardOwner.value="";
			txtbx_CrdtCardNumber.value="";
			txtbx_CrdtCardVerification_number.value="";
		}
	}
}

function HelpZipPopup(){
	window.open("help_zip.php","helper","width=300,height=150,top=200,left=350");
}


//function used to display the upgrade step1  entry forms
function fun_display_controls(clicked_ctrlname , to_display_hide_controlname)
{
    with(document.frmMerchantRegistration)
	{
	   if( elements[clicked_ctrlname].checked==true)
         document.getElementById(to_display_hide_controlname).style.display="";
	   else
	    document.getElementById(to_display_hide_controlname).style.display="none";
	 
	     if(clicked_ctrlname=="chkbx_increase_myaccountsize")
            fun_calculateprice(clicked_ctrlname);
	}
}

//function used to calculate charges and size in upgrade step1 
function fun_calculateprice(clicked_ctrlname)
{
 	with(document.frmMerchantRegistration)
    {
		StrMonth =  drpdwn_numofmonth.value;
		txt_amt_inusd.value = StrMonth*9;
    }
}

function showforms(stepno)
{
	for(index=1;index<=2;index++)
	{
		if(stepno==index)
		{
			if(document.getElementById("tr_step"+stepno).style.display=="none")
			document.getElementById("tr_step"+stepno).style.display="";
		}
		else
		{
			document.getElementById("tr_step"+index).style.display="none";

		}
	}
}

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


function validate_step1()
{
	with(document.frmMerchantRegistration)
    {			
		var adr_index_limit = hdn_MaxmiumAddress.value;
		var limit = parseInt(adr_index_limit - 1);
		var addressdisplaycount=0;
		for(adr_index=1;adr_index<limit;adr_index++)
		{
			if(document.getElementById("browse_tr"+adr_index).style.display!="none")
			{
				addressdisplaycount++;
			}
		}
		hdn_address_display_count.value = addressdisplaycount;

		if(txtbx_company_name.value=="")
		{
			alert("Please enter company name");
			txtbx_company_name.focus();
			return false;			
		}
		
		else if(txtbx_firstname.value=="")
		{
			alert("Please enter contact first name");
			txtbx_firstname.focus();
			return false;
		}
		else if(txtbx_lastname.value=="")
		{
			alert("Please enter contact last name");
			txtbx_lastname.focus();
			return false;
		}
		else if(txtbx_contact_email.value=="")
		{
			alert("Please enter contact email");
			txtbx_contact_email.focus();
			return false;
		}		
		else if(txtbx_username.value=="")
		{
			alert("Please enter user name");
			txtbx_username.focus();
			return false;
		}
		else if(txtbx_password.value=="")
		{
			alert("Please enter password");
			txtbx_password.focus();
			return false;
		}
		else if(txtbx_password.value!="" && (txtbx_password.value.length)<4)
		{
			alert("Password should be more than 4 characters");
			txtbx_password.focus();
			return false;
		}
		else if(txtbx_confirm_password.value=="")
		{
			alert("Please confirm your password");
			txtbx_confirm_password.focus();
			return false;
		}
		else if(txtbx_confirm_password.value!="" && txtbx_confirm_password.value!=txtbx_password.value)
		{
			alert("Passwords do not match");
			txtbx_confirm_password.focus();
			return false;
		}
		else
		{
			funAjaxVerifyMerchant();
		}
    }	
}

function validate_step2()
{
	with(document.forms["frmMerchantRegistration"])
    {
		
		if(company_logo.value!="")
		{
			filename=company_logo.value;
			if(filename.length>0)
			{
				var ary = filename.split(".");
				var ext = ary[ary.length-1].toLowerCase();
				if(ext=="jpg" || ext=="jpeg" || ext=="gif" || ext=="png")
				{
					hdn_register_mode.value="register";
					submit();
					return false;
				}
				else
				{
					alert("File type of  "+ext+"  is not allowed to upload");
					return false;
				}
			}
			else
			{
				alert("Please select only  jpg, jpeg, gif, png");
				return false;
			}
		}
		else
		{
			hdn_register_mode.value="register";
			submit();
			return false;
		}
	}	
}

function extcheck(filename)
{
	var ary = filename.split(".");
	var ext = ary[ary.length-1].toLowerCase();
	if(ext=="jpg" || ext=="jpeg" || ext=="gif" || ext=="png")
	{
		//do nothing just show the third form
		return true;
	}
	else
	{
		alert("File type of  "+ext+"  is not allowed to upload");
		return false;
	}
}

function validate_step1org()
{
	with(document.frmMerchantRegistration)
    {			
		var adr_index_limit = hdn_MaxmiumAddress.value;
		var limit = parseInt(adr_index_limit - 1);
		var addressdisplaycount=0;
		for(adr_index=1;adr_index<limit;adr_index++)
		{
			if(document.getElementById("browse_tr"+adr_index).style.display!="none")
			{
				addressdisplaycount++;
			}
		}
		hdn_address_display_count.value = addressdisplaycount;

		if(txtbx_company_name.value=="")
		{
			alert("Please enter company name");
			txtbx_company_name.focus();
			return false;			
		}
		
		
		else
		{
			funAjaxVerifyMerchant();
		}
    }	
}

function validate_step2org()
{
	with(document.forms["frmMerchantRegistration"])
    {
		
		if(company_logo.value!="")
		{
			filename=company_logo.value;
			if(filename.length>0)
			{
				var ary = filename.split(".");
				var ext = ary[ary.length-1].toLowerCase();
				if(ext=="jpg" || ext=="jpeg" || ext=="gif" || ext=="png")
				{
					hdn_register_mode.value="register";
					submit();
					return false;
				}
				else
				{
					alert("File type of  "+ext+"  is not allowed to upload");
					return false;
				}
			}
			else
			{
				alert("Please select only  jpg, jpeg, gif, png");
				return false;
			}
		}
		else
		{
			hdn_register_mode.value="register";
			submit();
			return false;
		}
	}	
}


function checkIt(evt) 
{
evt = (evt) ? evt : window.event
var charCode = (evt.which) ? evt.which : evt.keyCode
if (charCode > 31 &&(charCode!=255) && (charCode < 48 || charCode > 57) )
	{
	alert( "This field accepts numbers only.");
	return false
	}
}

//-->

//function used to verify the account details
function funAjaxVerifyMerchant()
{
  with(document.forms["frmMerchantRegistration"])
  {
    var strUserName=trimAll(txtbx_username.value);
	var strPostRegData="usrname="+strUserName;
	//show loading image 
	//document.getElementById("tr_reg_mem_loading").style.display="";
    
	 url="verify_merchant_account.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["frmMerchantRegistration"])
	{
		 if(strResponseTxt=="valid")
			 showforms("2");
		 else
			 document.getElementById("td_message").innerHTML="user name unavailable";
	}//with ends
  
  }//response check ends
}

//used in the edit company profile page
function validate_company_details(formname)
{
	with(document.forms[formname])
	{
		var adr_index_limit = hdn_MaxmiumAddress.value;
		var limit = parseInt(adr_index_limit - 1);
		var addressdisplaycount=0;
		for(adr_index=1;adr_index<limit;adr_index++)
		{
			if(document.getElementById("browse_tr"+adr_index).style.display!="none")
			{
				addressdisplaycount++;
			}
		}
		hdn_address_display_count.value = addressdisplaycount;

		if(txtbx_company_name.value=="")
		{
			alert("Please enter company name");
			txtbx_company_name.focus();
			return false;			
		}
		else if(txtbx_company_email.value=="")
		{
			alert("Please enter company email");
			txtbx_company_email.focus();
			return false;
		}
		else if(txtbx_company_email.value!="" && !validateEmail(txtbx_company_email.value))
		{
			alert("Please enter valid company email");
			txtbx_company_email.focus();
			return false;
		}
		else if(txtbx_firstname.value=="")
		{
			alert("Please enter contact first name");
			txtbx_firstname.focus();
			return false;
		}
		else if(txtbx_lastname.value=="")
		{
			alert("Please enter contact last name");
			txtbx_lastname.focus();
			return false;
		}
		else if(txtbx_contact_email.value=="")
		{
			alert("Please enter contact email");
			txtbx_contact_email.focus();
			return false;
		}
		else if(txtbx_contact_email.value!="" && !validateEmail(txtbx_contact_email.value))
		{
			alert("Please enter valid contact email");
			txtbx_contact_email.focus();
			return false;
		}
		else
		{
			hdn_mode.value="editcompany";
			submit();
			return false;
		}
	}
}

function validate_gardener_details(formname)
{
	with(document.forms[formname])
	{
		var adr_index_limit = hdn_MaxmiumAddress.value;
		var limit = parseInt(adr_index_limit - 1);
		var addressdisplaycount=0;
		for(adr_index=1;adr_index<limit;adr_index++)
		{
			if(document.getElementById("browse_tr"+adr_index).style.display!="none")
			{
				addressdisplaycount++;
			}
		}
		hdn_address_display_count.value = addressdisplaycount;

		if(txtbx_company_name.value=="")
		{
			alert("Please enter name");
			txtbx_company_name.focus();
			return false;			
		}
		
		else if(txtbx_firstname.value=="")
		{
			alert("Please enter contact first name");
			txtbx_firstname.focus();
			return false;
		}
		else if(txtbx_lastname.value=="")
		{
			alert("Please enter contact last name");
			txtbx_lastname.focus();
			return false;
		}
		else if(txtbx_contact_email.value=="")
		{
			alert("Please enter a valid email");
			txtbx_contact_email.focus();
			return false;
		}
		else if(txtbx_contact_email.value!="" && !validateEmail(txtbx_contact_email.value))
		{
			alert("Please enter a valid email");
			txtbx_contact_email.focus();
			return false;
		}
		else if(txtbx_city.value=="")
		{
			alert("Please enter your city");
			txtbx_city.focus();
			return false;
		}
		else
		{
			hdn_mode.value="editcompany";
			submit();
			return false;
		}
	}
}

function validate_company_details_admin(formname)
{
	with(document.forms[formname])
	{
		var adr_index_limit = hdn_MaxmiumAddress.value;
		var limit = parseInt(adr_index_limit - 1);
		var addressdisplaycount=0;
		for(adr_index=1;adr_index<limit;adr_index++)
		{
			if(document.getElementById("browse_tr"+adr_index).style.display!="none")
			{
				addressdisplaycount++;
			}
		}
		hdn_address_display_count.value = addressdisplaycount;

		if(txtbx_company_name.value=="")
		{
			alert("Please enter company name");
			txtbx_company_name.focus();
			return false;			
		}
		else
		{
			hdn_mode.value="editcompany";
			submit();
			return false;
		}
	}
}

function funaddPhoto(formname)
{
	with(document.forms[formname])
	{
		var photo_count = hdn_Photo_count.value;
		var photo_limit = hdn_Photo_Limit.value;
		if(photo_count>=photo_limit)
		{
			alert("You can upload only "+photo_limit+" photos");
			return false;
		}
		else
		{
			filename=company_photos.value
			if(filename!="" && extcheck(filename))
			{
				hdn_mode.value="add_photos";
				submit();
				return false;
			}
		}
	}
}

//function used to verify the account details
function PhotoProcess(rowid,process)
{
  with(document.forms["frmCompanyPhotos"])
  {
    var strphotoid=document.getElementById("hdn_photo_id_"+rowid).value;
	var strmerchantid = hdn_Merchant_id.value;
	var strPostRegData="photoid="+strphotoid+"&action="+process;
	//show loading image 
	document.getElementById("td_loading_message").style.display="";
    
		url="update_main_photo.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 = funResponsupdateMainPhoto;
			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 funResponsupdateMainPhoto()
{
  if(http_req.readyState== 4)
  {
	//Response text is=> "valid" or "invalid"
	var strResponseTxt= trimAll(http_req.responseText);	
	//show loading image 
	document.getElementById("td_loading_message").style.display="none";
   if(strResponseTxt=="updated")
	  {
		document.location="manage_company_photos.php";
	  }
  }//reponse check ends  
}


function deletelogo(formname)
{
	with(document.forms[formname])
	{
		if(confirm("Are you sure want to delete this image?"))
		{
			hdn_mode.value="deletelogo";
			submit();
			return false;
		}
	}
}
function addnewlogo(formname)
{
	with(document.forms[formname])
	{
		filename=company_logo.value;
		if(filename!="")
		{
			if(extcheck(filename))
			{
				hdn_mode.value="addlogo";
				submit();
				return false;
			}
		}
		else
		{
			return false;
		}
	}
}

//delete the banners
//function used to verify the account details
function BannerProcess(formname,process)
{
  with(document.forms[formname])
  {
	//show loading image 
	strPostRegData = "process="+process;
	document.getElementById("td_loading_message").style.display="";
    
	url="update_banner_status.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 = funResponsUpdateBannerStatus;
		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 funResponsUpdateBannerStatus()
{
  if(http_req.readyState== 4)
  {
	//Response text is=> "valid" or "invalid"
	var strResponseTxt= trimAll(http_req.responseText);	
	//show loading image 
	document.getElementById("td_loading_message").style.display="none";
   if(strResponseTxt=="updated")
	  {
		document.location="merchant_banners.php";
	  }
  }//reponse check ends  
}


function delete_merchant_banner(formname)
{
	with(document.forms[formname])
	{
		if(confirm("Are you sure to delete this banner?"))
		{
			hdn_mode.value="delete_banners";
			submit();
			return false;
		}
	}
}
function manage_banner(formname,mode)
{
	with(document.forms[formname])
	{
		filename = file_banner.value;
		bannerTempId = trimAll(hdn_BannerTemplateId.value);
		var funretvalue=false;

		if(filename!="" && bannerTempId!="")
		{
			alert("You can either upload your own file or create banner from templates.\nYou cannot do the both in same time.\n\nYou have already created the banner from templates.");
			return false;
		}
		else if(filename!="" && bannerTempId=="")
		{			
			/*if(extcheck(filename))
			{
				hdn_mode.value=mode;
				submit();
				return false;
			}*/

			   Extension=funGetFileExtension(filename);
			   if(Extension=="jpeg" || Extension=="jpg" || Extension=="gif" || Extension=="swf" || Extension=="png")
			   {
				    hdn_mode.value=mode;
					submit();
					return false;
			   }
			   else
			   {
				alert("Please select file of type swf, jpg, jpeg, png or gif");
				file_banner.focus();
				return false;
			   }
		}
		else if(filename=="" && bannerTempId!="")
		{
			hdn_mode.value=mode;
			submit();
			return false;
		}
		else if(filename=="" && bannerTempId=="")
		{
			if(mode=='add_banners')
			{
				alert("Please select swf, jpg, gif files to upload or select any banner templates.");
				return false;
			}
			else if(mode=='edit_banners')
			{
				hdn_mode.value=mode;
				submit();
				return false;
			}
		}
	}
}



function FunEditUsers(formname)
{
	with(document.forms[formname])
	{
		if(txtbx_name.value=="")
		{
			alert("Please enter your name");
			txtbx_name.focus();
			return false;
		}
		else if(txtbx_email.value=="")
		{
			alert("Please enter your email");
			txtbx_email.focus();
			return false;
		}
		else if(txtbx_email.value!="" && !validateEmail(txtbx_email.value))
		{
			alert("Please enter valid email");
			txtbx_email.focus();
			return false;
		}
		else if(txtbx_username.value=="")
		{
			alert("Please enter user name");
			txtbx_username.focus();
			return false;
		}

		else if(hdn_Process.value=='add' && new_password.value=="")
		{
			alert("Please enter a password");
			new_password.focus();
			return false;
		}
		else if(new_password.value!="" && (new_password.value.length)<4)
		{
			alert("Password should be more than 4 characters");
			new_password.focus();
			return false;
		}
		else if(new_password.value!="" && confirm_password.value=="")
		{
			alert("Please confirm your password");
			confirm_password.focus();
			return false;
		}
		else if(confirm_password.value!="" && confirm_password.value!=new_password.value)
		{
			alert("Passwords do not match");
			confirm_password.focus();
			return false;
		}	
		else
		{
			if((hdn_Process.value=='edit' || hdn_Process.value=='' ) && hdn_old_username.value == txtbx_username.value) 
			{
				hdn_mode.value='edit_profile';
				submit();
				return false;
			}
			else 
			{
				UserDetails(formname);
			}
		}
	}
}

//function used to verify the user details
function UserDetails(formname)
{
  with(document.forms[formname])
  {
	var strUserName=trimAll(txtbx_username.value);
	var strPostRegData="usrname="+strUserName;
  
	url="verify_merchant_account.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 = funResponsVerifyusers;
		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 funResponsVerifyusers(formname)
{
  if(http_req.readyState== 4)
  {
	//Response text is=> "valid" or "invalid"
	var strResponseTxt= trimAll(http_req.responseText);
	if(strResponseTxt=='invalid')
	  {
		document.getElementById("td_message").innerHTML="Username is not available";
		document.getElementById("txtbx_username").focus();
	  }
	else 
	  {
			FunUserProfileSubmit("frmUsersProfile");
	  }
  }//reponse check ends  
}

function FunUserProfileSubmit(formname)
{
	with(document.forms[formname])
	{
		if(hdn_Process.value=='edit')
			hdn_mode.value='edit_profile';
		else if(hdn_Process.value=='add')
			hdn_mode.value='add_profile';
		else if(hdn_Process.value=='')
			hdn_mode.value='edit_profile';
		submit();
		return false;
	}
}

//function to delete the manufacturer images and banners
function DeletePhoto(photo_id,formname)
{
	with(document.forms[formname])
	{
		if(confirm("Are you sure to delete this image?"))
		{
			hdn_mode.value='delete_photo';
			hdn_photo_id.value=photo_id;
			submit();
			return false;
		}
	}
}

function extcheckwithswf(filename)
{
	var ary = filename.split(".");
	var ext = ary[ary.length-1].toLowerCase();
	if(ext=="jpg" || ext=="jpeg" || ext=="gif" || ext=="png" || ext=="swf")
	{
		//do nothing just show the third form
		return true;
	}
	else
	{
		alert("File type of  "+ext+"  is not allowed to upload");
		return false;
	}
}
