var def_validatePostcode="1.0";
var postcodeCountries = new Array(); 
var postcodeFormats = new Array();

function updateSimpleUI(header, message)
{
	document.getElementById("errorDivisone").innerHTML=
        "<p class=\"error\"><strong>" + header + "</strong></p><p class=\"error\">" + message + "</p>";

    document.getElementById("errorDivisone").style.display="block";
   
}
function simplecheck(){
	
var intered_value = document.getElementById("city_zip_code").value;
var strcity_zip_code = fnTrim(intered_value, 2);
var countrycodeValues = document.drop_off_locator_simple.elements['user_location'].options[document.drop_off_locator_simple.elements['user_location'].selectedIndex].value;


if ( strcity_zip_code.length ==0 ||strcity_zip_code == "" || strcity_zip_code == null || strcity_zip_code.charAt(0) == ' ' )

    {
		
		updateSimpleUI(simpleHeaderMessage+"<br/>"+simpleNextHeaderMessage,"<br/>"+errorMessage1+"<br/>"+errorMessage2);
		return false;
		
    }


else if (document.getElementById("postcode_zip1").checked)
{
	
	
	if ( !postcode_checkFormat(countrycodeValues , strcity_zip_code ) )
	{
		errorString = pcode_makeError();
		updateSimpleUI(postalHeaderMessage,errorString);
		return false;
	}
	
}

  return true;
    
}
function postcode_checkFormat( country,postcode )
{
  
  if( postcode == "" ) return true;
  var countryExist = false;
  country = country.toUpperCase();
  temporaryArray = new Array();
  
  for (var j = 0; j < postcodeCountries.length; j++)
  {
      if( country == (postcodeCountries[j].toUpperCase()) )
      {
          countryExist = true;
          temporaryArray[temporaryArray.length] = postcodeFormats[j];
          if( pcode_equalAlphaNum( postcode, postcodeFormats[j] ) ) 
        	  {
        	  //alert("Inside postcode_checkFormat:: return true to your method sive postal code is valid " );
        	      return true;
        	  
        	  }
      }
  }
  if (countryExist == false)
  {
	  //alert("Inside postcode_checkFormat:: return true to your method" );
      return true;
  }
  //alert("Inside postcode_checkFormat:: return false to your method" );
  return false;
}

function pcode_equalAlphaNum( string1, string2 )
{
	
	string1 =fnTrim(string1, 2);
	string2 =fnTrim(string2, 2);
	
  if( string1.length != string2.length ) return false;
  sameChars = 0;
  for( j = 0 ; j < string1.length ; j++ )
  {
    c1 = string1.charAt( j )
    c2 = string2.charAt( j )
   
    if( ( charIsDigit( c1 ) && charIsDigit( c2 ) ) ||
        ( charIsLetter( c1 ) && charIsLetter( c2 ) ) ||
        string1.charAt( j ) == string2.charAt( j ) )
    {
    	
      sameChars++;
    
    }
  }
  if( sameChars == string1.length )
	  {
	
	  return true;
	  
	  }
 
  return false;
}


function pcode_makeError()
{
  errorMsg = postalErrorMessage1 + "<br/> " + postalErrorMessage2 + ": \n";
  comma = "";
  for( i = 0 ; i < temporaryArray.length ; i++ )
  {
    errorMsg += ( comma + "\"" + temporaryArray[i] + "\"" );
    comma = ", ";
  }
  return errorMsg;
}
function checkForCountry()
{
	
	///enable form fields if those are disabled already
	//claer if any error message present in scereen
	document.getElementById("errorDivisone").innerHTML=" ";
	enableSimpleFormField();
	//
	var countrycodeVal = document.drop_off_locator_simple.elements['user_location'].options[document.drop_off_locator_simple.elements['user_location'].selectedIndex].value;
	var homePagecode =homePagecountrycodesimple;
	var toolNewWindow = null;
	//changes
	var lang =  new Array();  
	var langURL = new Array(); 
	var currentpagelang= homePagelangcodesimple;
	var urlIndex=0;
	var langurlNotFound = true ;
	var exictiondone = false ;
	var english ="en";

	if( countrycodeVal == "" )
	{
		disbleSimpleFormField();
	}
	else if (countrycodeVal != null)
	{
		
			toolNewWindow = getURL(countrycodeVal, currentpagelang);
			//alert("inside toolSubmitWindow"+toolNewWindow);
			
			if (toolNewWindow != null && toolNewWindow != "" )
			{
				if((toolNewWindow != "mappoint" && toolNewWindow!= undefined) &&(toolNewWindow.indexOf("mappoint|")< 0))
				{

					// disble the forms field except go button//changes by suresh/21-oct-2009
                    //?	If the country selected does not support mappoint, disable the fields apart from the drop down and go button
                    disbleSimpleFormField();
                    
                    
                    //logic to open new window
                    //?	Open the result in same window for the home country which does not support mappoint
                    if(countrycodeVal != homePagecode)
                    {
					
						var indexexternal =toolNewWindow.indexOf("~");
	                	if(indexexternal>0)
	                	{
	                		var urlExternal= toolNewWindow.split("~");
	                	    window.open(urlExternal[1],'DHL');
	                	}
	                	else
	                	{
	                		window.open(toolNewWindow,'DHL');	
	                	}
    				
					
                    }
                    else
                    {
                    	var indexexternal =toolNewWindow.indexOf("~");
                    	if(indexexternal>0)
                    	{
                    		var urlExternal= toolNewWindow.split("~");

                    	    window.open(urlExternal[1],'DHL');
                    	}
                    	else
                    	{
                    		window.open(toolNewWindow,"_parent");
                        	
                    	}
                    	
                    }
					//window.open("http://www.dhl.com",'DHL');"_parent"

					
					
				}
			}
		
		
	}
}

function disbleSimpleFormField()
{
	//alert("hi inside disbleSimpleFormField function");
	//document.getElementById("city_town").disabled="true";
	//document.getElementById("postcode_zip1").disabled="true";
	//document.getElementById("city_zip_code").disabled="true";
	document.getElementsByName("city_zip")[0].disabled=true;
	document.getElementsByName("city_zip")[1].disabled=true;
	document.getElementsByName("city_zip_code")[0].disabled=true;
	document.getElementsByName("city_zip_code")[0].style.backgroundColor  = '#CDCDCD';

	
	
}

function enableSimpleFormField()
{
	//alert("hi inside enableSimpleFormField function");
	document.getElementsByName("city_zip")[0].disabled=false;
	document.getElementsByName("city_zip")[1].disabled=false;
	document.getElementsByName("city_zip_code")[0].disabled=false;
	document.getElementsByName("city_zip_code")[0].style.backgroundColor  = 'white';
	
}

function onLoadDisableSimpleForm()
{

	//alert("hi inside onLoadDisableSimpleForm function");
	var countryCodeSubmit1 = document.drop_off_locator_simple.elements['user_location'].options[document.drop_off_locator_simple.elements['user_location'].selectedIndex].value;
	var toolSubmitWindow1 = null;
	var homePagecode1=homePagecountrycodesimple;
	var currentpagelang= homePagelangcodesimple;
	//alert("homePagecode1:"+homePagecode1);
	
	if( countryCodeSubmit1 == "" )
	{
		disbleSimpleFormField();
	}
	else if(homePagecode1 != null)
	{
		toolSubmitWindow1 = getURL(countryCodeSubmit1, currentpagelang);
		//alert("toolSubmitWindow1:"+toolSubmitWindow1);
		if((toolSubmitWindow1 != "mappoint" && toolSubmitWindow1!= undefined )&& (toolSubmitWindow1.indexOf("mappoint|")< 0 &&toolSubmitWindow1!= undefined ))
		{
			// disble the forms field except go button//changes by suresh/21-oct-2009
            //?	If the country selected does not support mappoint, disable the fields apart from the drop down and go button
            if(countryCodeSubmit1 == homePagecode1)
            {
            disbleSimpleFormField();
            }
		}
	
	}
}

function onSubmitSimpleCheck()
{
	//alert("insde the onSubmitSimpleCheck");
	
	var countryCodeSubmit = document.drop_off_locator_simple.elements['user_location'].options[document.drop_off_locator_simple.elements['user_location'].selectedIndex].value;
	//alert("insde the onSubmitSimpleCheck with countryCodeSubmit--->"+countryCodeSubmit);
	var homePagecodeSubmit=homePagecountrycodesimple;
	var toolSubmitWindow = null;
	var isWindowOpen = true;
	
	var lang =  new Array();  
	var langURL = new Array(); 
	var currentpagelang= homePagelangcodesimple;
	var urlIndex=0;
	var langurlNotFound = true ;
	var exictiondone = false ;
	var english ="en";
	
	if( countryCodeSubmit == "" )
	{
		return false;
	}
	
	//check location is selected  or not 
	//alert("hi before "+countryCodeSubmit.length )
	if(countryCodeSubmit.length ==0)
	{
		updateSimpleUI(postalHeaderMessage,errorMessage3);
		return false;
		 
	}
	
	//alert("hi inside onSubmitSimpleCheck function");
	if(countryCodeSubmit != null)
	{
		toolSubmitWindow = getURL(countryCodeSubmit, currentpagelang);
		//alert("inside toolSubmitWindow"+toolSubmitWindow);
		//alert("inside toolSubmitWindow.indexOf--mappoint|--->"+toolSubmitWindow.indexOf("mappoint|"));
		
		if (toolSubmitWindow != null)
		{
			
			if((toolSubmitWindow != "mappoint" && toolSubmitWindow!= undefined )&&(toolSubmitWindow.indexOf("mappoint|")< 0 && toolSubmitWindow!= undefined))
			{

				// disble the forms field except go button//changes by suresh/21-oct-2009
                //?	If the country selected does not support mappoint, disable the fields apart from the drop down and go button
                disbleSimpleFormField();
                
                //logic to open new window
                //?	Open the result in same window for the home country which does not support mappoint
                if(countryCodeSubmit != homePagecodeSubmit)
                {
                	
    				
    				var indexexternal =toolSubmitWindow.indexOf("~");
                	if(indexexternal>0)
                	{
                		var urlExternal= toolSubmitWindow.split("~");
                	    window.open(urlExternal[1],'DHL');
                	}
                	else
                	{
                		window.open(toolSubmitWindow,'DHL');	
                	}
    				isWindowOpen = false;
    				
                }
                else
                {
                	
                	
                	var indexexternal =toolSubmitWindow.indexOf("~");
                	if(indexexternal>0)
                	{
                		var urlExternal= toolSubmitWindow.split("~");
                		//alert("open external link-->"+urlExternal[1]);
                	    window.open(urlExternal[1],'DHL');
                	    isWindowOpen = false;
                	}
                	else
                	{
                		window.open(toolSubmitWindow,"_parent");
                    	isWindowOpen = false;
                	}
                	
                	
                }
				
			}
			else
			{
				 return simplecheck();
			}
		}
	}
	else
	{
		 return simplecheck();
	}
	return isWindowOpen;
}

function CheckHttpUrl(strUrl)
{    
        
		//alert("strUrl:"+strUrl.toLowerCase());
        var RegexUrl = /(http|https|HTTP|HTTPS):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
        return RegexUrl.test(strUrl.toLowerCase());
}
function fnTrim(inputString, side)
{
	var tempString = "";
	var tempChar = "";
	var count = "";
	var SINGLE_BLANK = " ";
	var inputWidth = 0;
	if (null == inputString)
	{
	    tempString = "";
	}
	else
	{
		inputWidth = inputString.length;
		switch (side)
		{
		    case 0:
				//left trim
				for (count = 0; count < inputWidth; count++)
				{
				    tempChar = inputString.charAt (count);
				    if (SINGLE_BLANK != tempChar )
				    {
				        tempString = inputString.substring (count, inputWidth);
				        break;
				    }
				}
				break;

		    case 1:
	            //right trim
	            for (count = inputWidth - 1; count >= 0 ; count--)
	            {
	                tempChar = inputString.charAt (count);
	                if (SINGLE_BLANK != tempChar )
	                {
	                    tempString = inputString.substring (0, count+1);
	                    break;
	                }
	            }
	            break;
	        case 2:
	            //both trim
	            for (count = 0; count < inputWidth; count++)
	            {
	                tempChar = inputString.charAt (count);
	                if (SINGLE_BLANK != tempChar )
	                {
	                    tempString = inputString.substring (count, inputWidth);
	                    break;
	                }
	            }
	            inputWidth = tempString.length;
	            for (count = inputWidth - 1; count >= 0 ; count--)
	            {
	                tempChar = tempString.charAt (count);
	                if (SINGLE_BLANK != tempChar )
	                {
	                    tempString = tempString.substring (0, count+1);
	                    break;
	                }
	            }
	            break;
        }
    }
    return tempString;
} // end of fnTrim


function getURL(country, language)
{
	language = fnTrim(language, 2);
	//alert("Country -> " + country);
	//alert("Language -> " + language);
	
	var tool = "";
	var returnURL = "";
	var lang = "";
	var defLanguage = "en";
	var defaultreturnURL = ""
	var defaultFirstUrl = "";
	
	//alert("countryCode.length -> " + countryCode.length);
	
	 for (var i = 0; i < countryCode.length ; i++) 
		 {
	        if (countryCode[i] == country)
		    {
		         tool = mapPointLinkForCountry[i];
				 
				 //alert("countryCode[i] -> " + countryCode[i] + " -> " + tool);
				 //alert("tool.indexOf(,) -> " + tool.indexOf(","));
				 if( tool.indexOf(",") == -1 )
				 {
						var url = fnTrim(tool ,2);
					   var IndexofEqual =url.indexOf("=");
	                    
						if(IndexofEqual == 2)
						{
							lang = url.substr(0,IndexofEqual);
							//alert("Lang -> " + lang);
							//alert(lang[i]);
							returnURL  = url.substr(IndexofEqual+1 ,url.length);
							//alert("returnURL -> " + returnURL);
							//alert(langURL[i]);
						}
						else
						{
							returnURL=url;
							
							//alert("returnURL -> " + returnURL);
							
							//alert(langURL[i]);
							
						}
				 }
				 else
				 {
					var urls = tool.split(",");
					
					//alert( "urls.length -> " + urls.length);
					
					for (j=0; j<urls.length;j++ )
					{
						var url = fnTrim(urls[j] ,2);
						
						//alert( "URL: -> " + url);
						
					   var IndexofEqual =url.indexOf("=");
					   
					   //alert( "IndexofEqual: -> " + IndexofEqual);
	                    
						if(IndexofEqual == 2)
						{
							lang = url.substr(0,IndexofEqual);
							//alert( "lang: -> " + "|"+lang+"|");
							//alert( "language: -> " + "|"+language+"|");
							//alert(lang[j]);
							//this will hold first url if user does not mention any english lang 
							//he  mentoins  only the other lang like ( de=abc.com, fr=mg.com)
							if(j==0)
							{
								defaultFirstUrl = url.substr(IndexofEqual+1 ,url.length);
							}
							
							if ( lang == language )
							{
								returnURL =url.substr(IndexofEqual+1 ,url.length);
								//alert( "returnURL: -> " + returnURL);
								//alert(langURL[j]);
								
								break;
							}
							else if (lang == defLanguage)
							{
								defaultreturnURL =url.substr(IndexofEqual+1 ,url.length);
							}
							
						}
					}//end of loop
					
					if (returnURL == "")
					 {
						if(defaultreturnURL!="")
						{
						 returnURL =defaultreturnURL;
						}
						else
						{
							returnURL=defaultFirstUrl;
						}
					 }
					
				 }
		         
	        }
			
			if( returnURL != "" )
			{
			   break;
			}
	    
       }
	 
	   
	   //alert(" Return URL -> " + returnURL );
	   
	   return returnURL;
}

function fnDropOffCheckEnter(form)
{
	
	    ////clean the error message present in the screen/suresh 
	    document.getElementById("errorDivisone").innerHTML=" ";
	    return true;
	
}
//added by suresh

function checklandingpage()
{

    
   // enableSimpleFormField();
	
	
    //Added for DHL-1941
    var countrycodeValue  = "";
	
    countrycodeValue = document.drop_off_locator_simple.elements['user_location'].options[document.drop_off_locator_simple.elements['user_location'].selectedIndex].value;
	var currentpagelang= homePagelangcodesimple;
	var strpostcode_zip = document.getElementById("city_zip_code").value ;
	if( countrycodeValue == "" )
	{
		//Added for dhl-1941
		updateSimpleUI(postalHeaderMessage,errorMessage3);
		return false;
	}
	var url =  getURL(countrycodeValue, currentpagelang);
	//alert("hi suresh returned url--->"+url);
	//alert("redio button pin"+document.getElementById("postcode_zip1").checked);
	//alert("redio button city--->"+document.getElementById("city_town").checked);
	var trimedinput =fnTrim(strpostcode_zip, 2);
	//alert("value intered in text field/zip/city --->"+trimedinput +  trimedinput.length );
	//alert("now check the condition:"+url.indexOf("mappoint|")>= 0);
	if (url.indexOf("mappoint|")>= 0)
	{ 
		////alert("inside new flow");

	    if(trimedinput.length == 0 ||trimedinput == "" || trimedinput == null  )
	    {
	    	updateSimpleUI(simpleHeaderMessage+"<br/>"+simpleNextHeaderMessage,"<br/>"+errorMessage1+"<br/>"+errorMessage2);
			return false;
		}
	    //alert("inside new flow2");
		if (document.getElementById("postcode_zip1").checked && trimedinput!= "" && trimedinput!= undefined && trimedinput.length!=0)
		{ 
			///alert("inside new flow21");
			//alert("inside Zip");
			
			if ( !postcode_checkFormat(countrycodeValue , strpostcode_zip ) )
			{
				//alert("inside new flow22");
				errorString = pcode_makeError();
				updateSimpleUI(postalHeaderMessage,errorString);
				return false;
			}
			else
			{

			}
			
		}
		//alert("inside new flow3");
	
	}
	else
	{
		//do the alredy present flow 
		//alert("do the alredy present flow  NOW ");
		return onSubmitSimpleCheck();
	}
	
	 //var  countrycodeValue ="au";
	//alert("hi suresh 2");
	
	
	if (url.indexOf("mappoint|")>= 0)

	{
		var paramStr = ""
		
		if( document.getElementById("postcode_zip1").checked )
		{
			paramStr="&postcode_zip=";
		}
		else if( document.getElementById("city_town").checked )
		{
			paramStr="&city_town=";
		}
		
		//alert("processing started");
		var indexofSep = url.indexOf("|");
		var landingpageurl = url.substr(indexofSep+1 ,url.length)
		//form query string as given below 
		//http://206.236.141.53:7004/content/g0/en/express/shipping/find_dhl_locations.html?
		//drop_off_locator_location=au&postcode_zip=20000&simpleQuery=simpleQuery
		var URLtohit = landingpageurl+"?drop_off_locator_location="+countrycodeValue+paramStr+trimedinput+"&simpleQuery=simpleQuery";
		//alert("hi suresh final url to hit  url"+URLtohit);
		//open in new new window 
		 if(countrycodeValue != homePagecountrycodesimple)
         {
	        // //alert(" insidethe new window"+homePagecountrycodesimple);
			 window.open(URLtohit,'DHL');
			 return false
         }
		 else
		 {
			// //alert(" insidethe same window"+homePagecountrycodesimple);
			 window.open(URLtohit,"_parent");
			 return false
	     }
		
	}
	
}

function charIsDigit( ch )
{
	code = ch.charCodeAt( 0 );
	if( code >= 48 && code <= 57 ) return true;
	return false;
}
function charIsLetter( ch )
{
	code = ch.charCodeAt( 0 );
	if( code >= 65 && code <= 90 ) return true;
	if( code >= 97 && code <= 122 ) return true;
	return false;
}
function charIsNonAlphanumeric( ch )
{
	if( charIsLetter( ch ) || charIsDigit( ch ) )
	return false;
	return true;
}




