// Krishan Babbar: 18-Dec-07
//Email by Anurag with subject "Changes requested" dated Tuesday, December 18, 2007 10:25 AM

/*function convertToCapital(obj) {
	if (obj != undefined && obj.value != "") {
		//Anurag: removed the trim function as this will not allow user to add spaces between words of the company name
		obj.value = obj.value.toUpperCase();
	}
}
//anurag: 29-Feb-08
//this function will convert the text written to lower case.
function convertToLower(obj) {
	if (obj != undefined && obj.value != "") {
		obj.value = obj.value.toLowerCase();
	}
}*/
//Harnarayan 19_05_08 Function for removing % from company name
function removePercentSign(obj, event)
{
	if((event.keyCode<36 || event.keyCode >40) && (event.keyCode != 8 && event.keyCode != 46))
	{
		obj.value = obj.value.replace('%', '');
	}
}
//Harnarayan 19-05-08 Added event parameter and also associated check so that on pressing home, end, left or right buttons the functionality can work properly
function convertToCapital(obj, event) {
	if((event.keyCode<36 || event.keyCode >40) && (event.keyCode != 8 && event.keyCode != 46))
	{
		if (obj != undefined && obj.value != "") {
			//Anurag: removed the trim function as this will not allow user to add spaces between words of the company name
			obj.value = obj.value.toUpperCase();
		}
	}
}
//Harnarayan 19-05-08 Added event parameter and also associated check so that on pressing home, end, left or right buttons the functionality can work properly
function convertToLower(obj, event) {
	if((event.keyCode<36 || event.keyCode >40) && (event.keyCode != 8 && event.keyCode != 46))
	{
		if (obj != undefined && obj.value != "") {
			obj.value = obj.value.toLowerCase();
		}
	}
}
//Harnarayan 19_05_08 Function for removing % from company name
function removePercentSign(obj, event)
{
	if((event.keyCode<36 || event.keyCode >40) && (event.keyCode != 8 && event.keyCode != 46))
	{
		obj.value = obj.value.replace('%', '');
	}
}

//Used to duplicate billing information into shipping inforamtion 

function duplicatebillingdata(){
			
		var billtoAddress = document.getElementById('CompanyBilltoAddress').value;
		var billtoCity = document.getElementById('CompanyBilltoCity').value;
		var billtoState = document.getElementById('CompanyBilltoState').value;
		var billtoPostalCode = document.getElementById('CompanyBilltoPostalCode').value;
		var billtoCountry = document.getElementById('CompanyBilltoCountryId').value;
		var duplicate = document.getElementById("MemberCopyBilling").checked;

		if (duplicate == true) {
			document.getElementById('CompanyShiptoAddress').value = billtoAddress;
			document.getElementById('CompanyShiptoCity').value = billtoCity;
			document.getElementById('CompanyShiptoState').value = billtoState;
			document.getElementById('CompanyShiptoPostalCode').value = billtoPostalCode;
			document.getElementById('CompanyShiptoCountryId').value = billtoCountry;
			callUrl('/members/getcountrycode/', document.getElementById('CompanyShiptoCountryId').value);
		} 
		
		else { 
		    
			document.getElementById('CompanyShiptoAddress').value = '';
			document.getElementById('CompanyShiptoCity').value = '';
			document.getElementById('CompanyShiptoState').value = '';
			document.getElementById('CompanyShiptoPostalCode').value = '';
			document.getElementById('CompanyShiptoCountryId').value = '';
			 }
			
	}

// postalcode search

function searchExistingCityStateCode(section, siteURL)
{ 
	var cityValue = document.getElementById('Company'+section+'toCity').value;
	divId = section+'CityDiv';
	document.getElementById('Company'+section+'toCountryIdDiv').style.display = 'none';
	document.getElementById(divId).style.display = 'block';
	
	document.getElementById(divId).innerHTML = '';
	if(section=='Bill'){
		var displayImg=0;
	}else{
		var displayImg=1;
	}
	new Ajax.Updater(divId,siteURL + '/members/getcitystatepostalcode/'+section+'/'+cityValue, {asynchronous:true, evalScripts:true, loading:showLoadingImage(displayImg)});
}	

//Anurag: 31-Mar-08, added the function to display the wait image.
function showLoadingImage(showLoadingImageFor) {
	if(showLoadingImageFor == 0) {
		document.getElementById('cityStateProcessing').style.display='block';
	}
	if(showLoadingImageFor == 1) {
		document.getElementById('shipCityStateProcessing').style.display='block';
	}
	if(showLoadingImageFor == 2) {
		document.getElementById('resellerCityStateProcessing').style.display='block';
	}
	
}
//Anurag: 31-Mar-08, added the function to display the wait image.
function showLoadingImage(showLoadingImageFor) {
	if(showLoadingImageFor == 0) {
		document.getElementById('cityStateProcessing').style.display='block';
	}
	if(showLoadingImageFor == 1) {
		document.getElementById('shipCityStateProcessing').style.display='block';
	}
	if(showLoadingImageFor == 2) {
		document.getElementById('resellerCityStateProcessing').style.display='block';
	}
	
}
//Anurag: 31-Mar-08, added the function to hide the wait image.
function hideLoadingImage(showLoadingImageFor) {
	if(showLoadingImageFor == 0) {
		document.getElementById('cityStateProcessing').style.display='none';
	}

	if(showLoadingImageFor == 1) {
		document.getElementById('shipCityStateProcessing').style.display='none';
	}

	if(showLoadingImageFor == 2) {
		document.getElementById('resellerCityStateProcessing').style.display='none';
	}
}


//Anurag: modified by code on 2-Jan-08, added parameter fieldPrefix so as to make it more generalized.
function fillBoxData(dataToBeFilled, stateData, postalCodeData, section)
{ 
	document.getElementById('Company'+section+'toCity').value = dataToBeFilled;
	document.getElementById('Company'+section+'toState').value = stateData;
	document.getElementById('Company'+section+'toPostalCode').value = postalCodeData;
	//document.getElementById(section+'CityDiv').style.display = 'none';
	//document.getElementById('Company'+section+'toCountryIdDiv').style.display = '';
	 $('#suggestions_' + section).hide();
}

//code changed by anurag on 3rd Jan 08, added parameter as fieldPrefix, so as to make this function more generalized.
function hide_city_div(section)
{
	if (document.getElementById(section).style.display == 'block') {
		document.getElementById(section).innerHTML = '';
		document.getElementById(section).style.display = 'none';
		document.getElementById('Company'+section.substring(0,4)+'toCountryIdDiv').style.display = '';
		if(section.substring(0,4) == 'Bill'){
			document.getElementById('cityStateProcessing').style.display='none';
		}

		if(section.substring(0,4) == 'Ship') {
			document.getElementById('shipCityStateProcessing').style.display='none';
		}
		//document.getElementById(section+'CountryId').style.display = '';
	}
}

/*function hide_state_div(prefix, sectionId)
{
	if (document.getElementById(prefix+'_existing_state').style.display == 'block') {
		document.getElementById(prefix+'_existing_state').innerHTML = '';
		document.getElementById(prefix+'_existing_state').style.display = 'none';
		document.getElementById('Company'+sectionId+'CountryId').style.display = '';
	}
}

function hide_postal_code_div(prefix, sectionId)
{
	if (document.getElementById(prefix+'_existing_postal_code').style.display == 'block') {
		document.getElementById(prefix+'_existing_postal_code').innerHTML = '';
		document.getElementById(prefix+'_existing_postal_code').style.display = 'none';
		document.getElementById('Company'+sectionId+'CountryId').style.display = '';
	}
}*/

function getoutsourcecountrycode(outsourcecompany_country_div, businessCountryCodeDiv,faxCountryCodeDiv,afterCountryCodeDiv,directCountryCodeDiv,countryId) {
	if (!isEmpty(countryId)) {
		new Ajax.Updater(outsourcecompany_country_div,'/outsourcecompanies/companyphonecountrycode/' + countryId, {
                
                onSuccess:function()
                {
					new Ajax.Updater(businessCountryCodeDiv,'/outsourcecompanies/businessphonecountrycode/' + countryId, {asynchronous:true, evalScripts:true});
                	new Ajax.Updater(faxCountryCodeDiv,'/outsourcecompanies/businessfaxcountrycode/' + countryId, {asynchronous:true, evalScripts:true});
					new Ajax.Updater(afterCountryCodeDiv,'/outsourcecompanies/afterhourscountrycode/' + countryId, {asynchronous:true, evalScripts:true});
					new Ajax.Updater(directCountryCodeDiv,'/outsourcecompanies/directphonecountrycode/' + countryId, {asynchronous:true, evalScripts:true});
                },
                
                asynchronous:true, evalScripts:true}
        );
	}
}

function checkNoBacks()
{
 if(document.getElementById('MemberIsReseller').checked == true)
	{ /*
	  if(document.getElementById('td_contact_detail') != null)			
		document.getElementById('td_contact_detail').style.background = '#DB9C61';
		
	  if(document.getElementById('td_quote_for') != null)			
		document.getElementById('td_quote_for').style.background = '#DB9C61';
	
	  if(document.getElementById('td_search_company') != null)			
		document.getElementById('td_search_company').style.background = '#DB9C61';
	  
      if(document.getElementById('td_company_detail') != null)			
		document.getElementById('td_company_detail').style.background = '#DB9C61';
		
      if(document.getElementById('td_contact_person') != null)			
		document.getElementById('td_contact_person').style.background = '#DB9C61';
	
      if(document.getElementById('td_contact_phone') != null)			
		document.getElementById('td_contact_phone').style.background = '#DB9C61';
	  
      if(document.getElementById('td_billing_shipping') != null)			
		document.getElementById('td_billing_shipping').style.background = '#DB9C61';
	  
	  if(document.getElementById('td_miscellaneous_details') != null)			
		document.getElementById('td_miscellaneous_details').style.background = '#DB9C61';
	  
	  if(document.getElementById('td_login_detail') != null)			
		document.getElementById('td_login_detail').style.background = '#DB9C61';
	*/
	
	}else{ /*
		
		if(document.getElementById('td_contact_detail') != null)			
		document.getElementById('td_contact_detail').style.background = '#FE0000';
		
		if(document.getElementById('td_quote_for') != null)			
		document.getElementById('td_quote_for').style.background = '#FE0000';
		
		if(document.getElementById('td_search_company') != null)			
		document.getElementById('td_search_company').style.background = '#FE0000';
		
		if(document.getElementById('td_company_detail') != null)			
		document.getElementById('td_company_detail').style.background = '#FE0000';
		
		if(document.getElementById('td_contact_person') != null)			
		document.getElementById('td_contact_person').style.background = '#FE0000';
		
		if(document.getElementById('td_contact_phone') != null)			
		document.getElementById('td_contact_phone').style.background = '#FE0000';
		
		if(document.getElementById('td_billing_shipping') != null)			
		document.getElementById('td_billing_shipping').style.background = '#FE0000';
		
		if(document.getElementById('td_miscellaneous_details') != null)			
		document.getElementById('td_miscellaneous_details').style.background = '#FE0000';

		if(document.getElementById('td_login_detail') != null)			
		document.getElementById('td_login_detail').style.background = '#FE0000';
		*/		
	}

}
//Harnarayan 09/06/08 function for validating
function validURL(url){
	var elval= url.value.replace(/[\n\r\s]+/,"");
	if(elval =="") return true;
	var str=url.value;
	if(1){
		var filter=/^((ht|f)tp(s?))(:((\/\/)(?!\/)))(((w){3}\.)?)([a-zA-Z0-9\-_\.]+(\.(com|edu|gov|int|mil|net|org|biz|au|info|name|pro|museum|co\.uk|in)))((?!\/))(([a-zA-Z0-9\-_\/]*)?)$/i      
		var filter1=/^(((w){3}\.)?)([a-zA-Z0-9\-_\.]+(\.(com|edu|gov|int|mil|net|org|biz|au|info|name|pro|museum|co\.uk|in)))((?!\/))(([a-zA-Z0-9\-_\/]*)?)$/i      
	}
	if (filter.test(str) || filter1.test(str))
		testresults=true;
	else
		testresults=false;
		
	return (testresults);
}