// JavaScript Document
function go(x)
{
	if(x=='suq')
     val=document.frminp.suq.value;
	 else if(x=='job')
	 val=document.frminp.jobq.value;
    else 
     val=document.frminp.q.value;	


if(val=='')
   {
       alert("Please enter the search keyword");
       return false;
   }
   if(isWhitespace(val))
   {
       alert("Blank space is not allowed");
       return false;
   }
   return true;
}
function submitLocal()
{
	if(go('suq'))
	{
             document.frminp.local.value="1";
		      document.frminp.adq.value=document.frminp.suq.value;
			  q=trim(document.frminp.suq.value);
			  q1 =  q.replace(/[^a-zA-Z 0-9]/g,'');
		      var q1=replaceAll(q1," ","_");
              var q1=replaceAll(q1,"+","_");  
		   	  document.frminp.action=search_url+q1+"/1.html";
              document.frminp.submit();
	}
}
function submitCustom()
{
	 if(document.frminp.ss.value==4)
	 {
	  chk='job';
           if(document.frminp.jobcity.value !='s')
           {
	     q = document.frminp.jobq.value +" "+ document.frminp.jobcity.value;
             q =urlencode(q);
           }
           else
           q = document.frminp.jobq.value;
	 }
	 else
	 {
	   chk='q';
	    q= document.frminp.q.value;
	 }
      key=trim(q);
      document.cse.frs.value="n";
    if(go(chk))
	{
       if(document.frminp.ss.value==2 || document.frminp.ss.value==6)   
	   {
      	 	  frnkey_st=0;
		  at_fr1=0; 
                  key=trim(document.frminp.q.value);
                  flen = key.length;
		  last=flen-1;
                  for(i=0; i<flen; i++)
                  {
                      schar = key.charAt(i);
		      if(i==0  && schar=='"' )
                      {
                             at_fr1=1;
                      }
                      if(schar==" ")
                      {
                          frnkey_st=1;
                      }
                   }
                   if(frnkey_st==1 && at_fr1!=1)
                   {
                      key='"'+trim(document.frminp.q.value)+'"';
                      document.cse.frs.value="y";
                   }
			if(document.frminp.ss.value==2)	   
		     document.cse.cx.value="008053485682220265615:uz6xwms9hb0";
			else
		     document.cse.cx.value="008053485682220265615:n9p_ljrnhgo";
	    }
	else if(document.frminp.ss.value==4)
	        document.cse.cx.value="008053485682220265615:k22o5glb8ym";
        else if(document.frminp.ss.value==5)
		 document.cse.cx.value="008053485682220265615:tl93-0wwy9i";
	else if(document.frminp.ss.value==7)
		 document.cse.cx.value="008053485682220265615:pqjgvnh54vu";
	else
		 document.cse.cx.value="008053485682220265615:uz6xwms9hb0";

	document.cse.q.value=key;
        document.cse.ss.value=document.frminp.ss.value;
        document.cse.hl.value=document.frminp.hl.value;
	document.cse.action=search_url+"search_custom.php";
        document.cse.submit();  
 }		
		
}
function submitCommon()
{
    if(go('q'))
	{
		q = document.frminp.q.value;
		 if(document.frminp.hl.value=="en")
		 {
 			   document.frminp.adq.value=document.frminp.q.value;
			   q1 =  q.replace(/[^a-zA-Z 0-9]/g,'');
		       q1 = replaceAll(q1," ","_");
			   q1 = replaceAll(q1,"+","_");  
		 }
		 else
		 {
		   q1=q;
		 } 
			   document.frminp.action=search_url+q1+"/1.html";
			   document.frminp.submit();
		}	   
}
function submitCeleb()
{
      // document.frminp.ss=8;
       q=document.frminp.q.value;
       q1 =  q.replace(/[^a-zA-Z 0-9]/g,'');
       var q1=replaceAll(q1," ","_");
       var q1=replaceAll(q1,"+","_");
       celeb_name=q1;  
       document.frminp.action=search_url+"celebrity/search/"+celeb_name+".html" ;
       document.frminp.submit();
}
function frmsub()
{
  frm_val=frmsubmit[document.frminp.ss.value];
  switch(frm_val)
  {
    case '0' : 
	       submitCommon();
		   break;
	case '1' :
	       submitCustom();
		   break;
	case '2' : 
	       submitLocal();
		   break;
          case '3' :
               submitCeleb();
		   break;
	default:
	       submitCommon();
		   break; 	   	   
   }	
}	
function setlanguage()
{
   document.getElementById('hl').value="en";
}
function callbus(x,y)
{
       document.frminp.local.value="1";
	   document.frminp.city.value=x;
	   document.frminp.catid.value=y;
   	   document.frminp.adq.value=document.frminp.suq.value;
	   q= urlencode(document.frminp.suq.value);
	   document.frminp.action=search_url+q+"/1.html";
	   document.frminp.submit();
	   
}
function callctpg(x)
{
       document.frminp.local.value="1";
	    document.frminp.adq.value=document.frminp.suq.value;
	   q= urlencode(document.frminp.suq.value);	
	    q=trim(document.frminp.suq.value);
			  q1 =  q.replace(/[^a-zA-Z 0-9]/g,'');
		      var q1=replaceAll(q1," ","_");
              var q1=replaceAll(q1,"+","_");  
	   document.frminp.action=search_url+q1+"/"+x+".html";
	   document.frminp.submit();
}
function callbspg(x)
{
       document.frminp.local.value="1";
	   	    document.frminp.adq.value=document.frminp.suq.value;
	   q= urlencode(document.frminp.suq.value);
	    q=trim(document.frminp.suq.value);
			  q1 =  q.replace(/[^a-zA-Z 0-9]/g,'');
		      var q1=replaceAll(q1," ","_");
              var q1=replaceAll(q1,"+","_");  
	   document.frminp.action=search_url+q1+"/"+x+".html";
	   document.frminp.submit();
}
function callsug()
{
	 q= urlencode(document.frminp.sug.value);  
     // document.frminp.action=search_url+q+"/"+document.frminp.hl.value+"_"+document.frminp.ss.value+"/"+"1.html";
	 document.frminp.action=search_url+q+"/"+"1.html";
	 document.frminp.submit();
}
function callpg(x)
{
	 document.frminp.adq.value=document.frminp.qry.value;
	 q  = trim(document.frminp.qry.value);
	 if(document.frminp.hl.value=='en')
	 {
	 q1 = q.replace(/[^a-zA-Z 0-9]/g,'');
     q1 = replaceAll(q1," ","_");
     q1 = replaceAll(q1,"+","_");  
	 }
	 else
	 {
		 q1=q;
	 }
	 document.frminp.action=search_url+q1+"/"+x+".html";
	 document.frminp.submit();
}
	
function urlencode( str ) {
                            
    var histogram = {}, unicodeStr='', hexEscStr='';
    var ret = (str+'').toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    histogram['\u00DC'] = '%DC';
    histogram['\u00FC'] = '%FC';
    histogram['\u00C4'] = '%D4';
    histogram['\u00E4'] = '%E4';
    histogram['\u00D6'] = '%D6';
    histogram['\u00F6'] = '%F6';
    histogram['\u00DF'] = '%DF';
    histogram['\u20AC'] = '%80';
    histogram['\u0081'] = '%81';
    histogram['\u201A'] = '%82';
    histogram['\u0192'] = '%83';
    histogram['\u201E'] = '%84';
    histogram['\u2026'] = '%85';
    histogram['\u2020'] = '%86';
    histogram['\u2021'] = '%87';
    histogram['\u02C6'] = '%88';
    histogram['\u2030'] = '%89';
    histogram['\u0160'] = '%8A';
    histogram['\u2039'] = '%8B';
    histogram['\u0152'] = '%8C';
    histogram['\u008D'] = '%8D';
    histogram['\u017D'] = '%8E';
    histogram['\u008F'] = '%8F';
    histogram['\u0090'] = '%90';
    histogram['\u2018'] = '%91';
    histogram['\u2019'] = '%92';
    histogram['\u201C'] = '%93';
    histogram['\u201D'] = '%94';
    histogram['\u2022'] = '%95';
    histogram['\u2013'] = '%96';
    histogram['\u2014'] = '%97';
    histogram['\u02DC'] = '%98';
    histogram['\u2122'] = '%99';
    histogram['\u0161'] = '%9A';
    histogram['\u203A'] = '%9B';
    histogram['\u0153'] = '%9C';
    histogram['\u009D'] = '%9D';
    histogram['\u017E'] = '%9E';
    histogram['\u0178'] = '%9F';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
 
    for (unicodeStr in histogram) {
        hexEscStr = histogram[unicodeStr];
        ret = replacer(unicodeStr, hexEscStr, ret); // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
}
function replaceAll(oldString, oldValue, newValue)
{
var newString = oldString.replace(oldValue, newValue);
if( newString.indexOf(oldValue)!= -1)
return replaceAll( newString, oldValue, newValue );
else
return newString;
}
function trim(inputString) {
		if (typeof inputString != "string") { return inputString; }
			var retValue = inputString;
			var ch = retValue.substring(0, 1);
			while (ch == " ") { // Check for spaces at the beginning of the string
				retValue = retValue.substring(1, retValue.length);
				ch = retValue.substring(0, 1);
			}
			ch = retValue.substring(retValue.length-1, retValue.length);
			while (ch == " ") { // Check for spaces at the end of the string
				retValue = retValue.substring(0, retValue.length-1);
				ch = retValue.substring(retValue.length-1, retValue.length);
			}
			while (retValue.indexOf("  ") != -1) { 
						retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
			}
			return retValue; // Return the trimmed string back to the user
	} 
function openWin()
{
 window.open(search_url+"sharefriend.php",null, "height=280,width=280,status=yes,toolbar=no,menubar=no,location=no,left=250,top=250");
}
