/*
  File Name    : Common.js.
  Purpose      : Common Js Function are present Here.
  Developer    : Dhanaraj.
  Date         : 04-02-2010.
*/

//Get the docroot form js Url
var scripts = document.getElementsByTagName("script");
var thisScript = scripts[scripts.length-1];
var thisScriptsSrc = thisScript.src;

 function get_hostname_from_url(url) {
     return url.match(/:\/\/(.[^/]+)/)[1];
 }
  
this.hostname = 'http://'+get_hostname_from_url(thisScriptsSrc);

// Ajax Basic Functions Starts here..
agent = navigator.userAgent;
var responseVal;
var xmlhttp 

if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
  try {
	xmlhttp = new XMLHttpRequest ();
  }
  catch (e) {
  xmlhttp = false;
  }
}

function myXMLHttpRequest ()
{
  var xmlhttplocal;
  try {
  	xmlhttplocal = new ActiveXObject ("Msxml2.XMLHTTP")}
  catch (e) {
	try {
	xmlhttplocal = new ActiveXObject ("Microsoft.XMLHTTP")}
	catch (E) {
	  xmlhttplocal = false;
	}
  }

  if (!xmlhttplocal && typeof XMLHttpRequest != 'undefined') {
	try {
	  var xmlhttplocal = new XMLHttpRequest ();
	}
	catch (e) {
	  var xmlhttplocal = false;
	}
  }
  return (xmlhttplocal);
}

// Ajax Basic Functions Ends here..

var mnmxmlhttp = Array ();
var xvotesString = Array ();
var mnmPrevColor = Array ();
var responsestring = Array ();
var myxmlhttp = Array ();
var responseString = new String;
var previd = -1;

/*
  Function Name: loadurl.
  Purpose      : Load the inner html details of particular element using the ajax.
  Developer    : Dhanaraj.
  Date         : 04-02-2010. */

function loadurl(url,divid){ // url-posturl,content - poststring,id=newdiv,target2=olddiv
 var response;
	if(!xmlhttp){
		xmlhttp = new myXMLHttpRequest ();		
	}
	if (xmlhttp) {
		xmlht = new myXMLHttpRequest ();
		if (xmlht) {
			try{
			xmlht.open ("POST", url, true);
			xmlht.setRequestHeader ('Content-Type','application/x-www-form-urlencoded');
			xmlht.send ("");
			errormatch = new RegExp ("^ERROR:");
			xmlht.onreadystatechange = function () {
				if (xmlht.readyState == 4) {
					response = xmlht.responseText;
					
				if (divid=='ca')
                                        {
                                            if(response!='F')
                                              {
                                                document.registration_form.action="/user/adduser.php";
				        document.registration_form.submit();  
                                              }
                                            else
                                              {
					document.getElementById(divid).innerHTML ="Captcha Not Valid";
                                                                                             
                                              }
				}                                        
					
				else
				{
				 document.getElementById(divid).innerHTML =response;
				}	
                                       
				                                       
				}
			}
			}/*** try***/
			catch(e){
				//alert("Errpr" + e);
			}
		}
           
	}
	return response;  
}

/*
  Function Name: validate.
  Purpose      : Validating the user registration Fields.
  Developer    : Dhanaraj.
  Date         : 04-02-2010.

*/
function validate()
{
// Declaration 
 var s=1;var x=1;
 var type=document.getElementById('role').value;
 var username=document.getElementById('username2').value;
 var firstname=document.getElementById('firstname').value;
 var lastname=document.getElementById('lastname').value;
 var unameavilable=document.getElementById('unameavilable').value;
 var password=document.getElementById('password2').value;
 var repassword=document.getElementById('repassword').value;
 var email=document.getElementById('email').value;
 var emailavilable=document.getElementById('emailavilable').value;
 var mobile=document.getElementById('mobile').value;
 var address=document.getElementById('address').value;
 var country=document.getElementById('country').value;
 var captcha=document.getElementById('number').value;
 var illegalChars = /\W/;

// Clear the innerHtml values
document.getElementById('unameerror1').innerHTML="";
document.getElementById('firstnameerror').innerHTML="";
document.getElementById('lastnameerror').innerHTML="";
document.getElementById('passworderror1').innerHTML="";
document.getElementById('repassworderror').innerHTML="";
document.getElementById('addresserror').innerHTML="";
document.getElementById('mobileerror').innerHTML="";
document.getElementById('countryerror').innerHTML="";
document.getElementById('emailerror').innerHTML="";

if(document.getElementById('cityerror')!=null)
{
document.getElementById('cityerror').innerHTML="";
}
if(document.getElementById('shoperror')!=null)
{
document.getElementById('shoperror').innerHTML="";
}
if(document.getElementById('ctypeerror')!=null)
{
document.getElementById('ctypeerror').innerHTML="";
}

// Username validation Starts here..
 	if(username.length==0)
	{ 
		document.getElementById('unameerror1').innerHTML="Enter Username";   
 		s=0;
	}
	else if(username.length > 20 || username.length < 5)
	{
  		document.getElementById('unameerror1').innerHTML="UserName must 5 to 20 Characters";
		s=0;
	}
	else if (illegalChars.test(username))
	{
 		document.getElementById('unameerror1').innerHTML="Special Characters not allowed.. [Numbers,Characters,underscore only Allowed]";
		s=0;
	}
        	else if (unameavilable=="Username Already Exists..")
        	{
          	s=0;
	}

// Username validation ends here..

//firstname and lastname validations start here
 	if(firstname.length==0)
	{ 
		document.getElementById('firstnameerror').innerHTML="Enter Firstname";   
 		s=0;
	}
	else if(firstname.length > 20 || firstname.length < 5)
	{
  		document.getElementById('firstnameerror').innerHTML="Firstname must 5 to 20 Characters";
		s=0;
	}
	else if (illegalChars.test(firstname))
	{
 		document.getElementById('firstnameerror').innerHTML="Special Characters not allowed.. [Numbers,Characters,underscore only Allowed]";
		s=0;
	}
	
 	if(lastname.length==0)
	{ 
		document.getElementById('lastnameerror').innerHTML="Enter Lastname";   
 		s=0;
	}
	else if(lastname.length > 20 || lastname.length < 5)
	{
  		document.getElementById('lastnameerror').innerHTML="Lastname must 5 to 20 Characters";
		s=0;
	}
		else if (illegalChars.test(lastname))
	{
 		document.getElementById('lastnameerror').innerHTML="Special Characters not allowed.. [Numbers,Characters,underscore only Allowed]";
		s=0;
	}	
//firstname and lastname validations end here

// Password validation Starts here..

	if(password.length==0)
	{ 
		document.getElementById('passworderror1').innerHTML="Enter Password";
		s=0;
	}
	else if(password.length > 10 || password.length < 6)
	{
		 document.getElementById('passworderror1').innerHTML="Password must 6 to 10 Characters";
		s=0;
	}
	
	if(repassword.length==0)
	{ 
		document.getElementById('repassworderror').innerHTML="Enter Re-Password";
		s=0;
	}
	else if(repassword!=trim(password))
	{  
		document.getElementById('repassworderror').innerHTML="Password doest not match";
		s=0;
	}
	
// Password validation ends here.

// Email validation Starts here.

	if(email.length==0)
	{ 
		document.getElementById('emailerror').innerHTML="Enter Email-Address";
		s=0;
	}
        else if (emailavilable=="Email Already Exists..")
        {
          	s=0;
	}
	else
	{
		var tfld = trim(email);                        // value of field with whitespace trimmed off
   		var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    		illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

  		if (!emailFilter.test(tfld)) 
 		{
 			 document.getElementById('emailerror').innerHTML="Enter Valid Email-Address";
			 s=0;
		}
 		else if (email.match(illegalChars))
 		{   
 			 document.getElementById('emailerror').innerHTML="The email address contains illegal characters";
			s=0;
		}
	}
// Email validation ends here.


// Address validation Starts here.

	if(address.length==0)
	{ 
		document.getElementById('addresserror').innerHTML="Enter Address";
		s=0;
	}
	else if(address.length > 100 || address.length < 6)
	{
		document.getElementById('addresserror').innerHTML="Address must 6 to 100 Characters";
		s=0;
	}

// Address validation ends here.

// Mobile number validation Starts here.

	var stripped = mobile.replace(/[\(\)\.\-\ ]/g, '');  
	if(mobile.length==0)
	{ 
		document.getElementById('mobileerror').innerHTML="Enter MobileNumber";
		s=0;
	}
	else if (isNaN(parseInt(stripped)))
	{
		document.getElementById('mobileerror').innerHTML="The phone number contains illegal characters";
		s=0;
	}
	else if (!(stripped.length == 10))
	{
   		 document.getElementById('mobileerror').innerHTML="The phone number is the wrong length. Make sure you included an area code";
		s=0;
	}
// Mobile Number validation ends here.

 	if(country=="-2")
            {
              document.getElementById('countryerror').innerHTML="Choose the Country..";
              s=0;x=0;
            }


// Based on the Role Validations Starts ...
        if(type=="CM")
        {   
           if(x==1)
           {
            var city=document.getElementById('city').value;
            if(city=="-2")
            {
             document.getElementById('cityerror').innerHTML="Choose the City..";
             s=0;
            }
          }
	}

        if(type=="CM")
        {
	if(city!="-2")
	{var shop=document.getElementById('shop').value;
	if(shop=="-2")
	{
             document.getElementById('shoperror').innerHTML="Choose the Shop Name..";
             s=0;	 
             }
	}        
        }	
       else if(type=="SA")
        {
 	   var shopname=document.getElementById('shopname').value;
           if(shopname=="-2")
            {
              document.getElementById('shoperror').innerHTML="Choose the Shop..";
              s=0;
            }
	}
       else if(type=="G")
        {
 	   var ctype=document.getElementById('ctype').value;
           if(ctype=="-2")
            {
              document.getElementById('ctypeerror').innerHTML="Choose the Coopon type..";
              s=0;
            }
	}	
// Based on the Role Validations Ends ...       

// form Submission Starts here..
//s=1;

	if(s==1)
	{ 
		var encrypted;
		encrypted = hex_md5(document.registration_form.password.value); // Ecrypting the password.
		document.registration_form.enc_password.value = encrypted;
	          loadurl(this.hostname+"/user/captchavalidation.php/?no="+captcha,'ca');
		
	}
// form Submission ends here..
}

function wait(msecs)
{
var start = new Date().getTime();
var cur = start
while(cur - start < msecs)
{
cur = new Date().getTime();
}
}

/*
  Function Name: loadcountry.
  Purpose      : load the city based on the Country.
  Developer    : Dhanaraj.
  Date         : 04-02-2010.

*/
function loadcountry(val,type)
{
  if(val!='-2')
  { 
   if(document.getElementById('citytag')!=null)
	{
	document.getElementById('citytag').innerHTML=''; 
	 } 
     if(type=="cm")
       loadurl(this.hostname+"/user/loadstate.php?countrycode="+val,"dynamiclocation");
      else
	loadurl(this.hostname+"/user/loadstate.php?countrycode="+val+"&type=n","dynamiclocation");
 }
}

/*
  Function Name: loadsubtype.
  Purpose      : load the coupon Subtype Based on the Coupon Type.
  Developer    : Dhanaraj.
  Date         : 24-02-2010.

*/
function loadsubtype(val)
{
  if(val!='-2')
  { 
   if(document.getElementById('subtypetag')!=null)
	{
	document.getElementById('subtypetag').innerHTML=''; 
	 } 
     
       loadurl(this.hostname+"/user/loadstate.php?subtype="+val,"subtypelocation");
     
 }
}

/*
  Function Name: loadshop.
  Purpose      : load the Shop id based on the Shop Details.
  Developer    : Dhanaraj.
  Date         : 04-02-2010.

*/
function loadshop(val)
{
	loadurl(this.hostname+"/user/loadstate.php?shopcode="+val,"shopdetails");
}

function load_cooponshopdetails(val)
{

document.getElementById('shopnamelist').innerHTML='';
	loadurl(this.hostname+"/user/loadstate.php?citycode="+val,"shopnamelist");
}

function loadshopdetails(val)
{
  
	loadurl(this.hostname+"/user/loadstate.php?shopcodedetails="+val,"lshopdetails");
}

/*
  Function Name: loadcityshop.
  Purpose      : load the City id based on the Shop Details.
  Developer    : Dhanaraj.
  Date         : 04-02-2010.

*/
function loadcityshop(val)
{
if(val!='-2')
  { 
   if(document.getElementById('shoptag')!=null)
	{
	document.getElementById('shoptag').innerHTML=''; 
	 }
  
	loadurl(this.hostname+"/user/loadstate.php?citycode="+val,"cityshopdetails");
	}
}
/*
  Function Name: checkusername.
  Purpose      : Checking the user name avilable in db or not.
  Developer    : Dhanaraj.
  Date         : 04-02-2010.

*/
function checkusername(val)
{  
  if(trim(val).length!=0)
  loadurl(this.hostname+"/user/adduser.php?type="+val,"unameavilable");
}
/*
  Function Name: checkemail.
  Purpose      : Checking the user email avilable in db or not.
  Developer    : Dhanaraj.
  Date         : 04-02-2010.

*/
function checkeamil(val)
{
if(trim(val).length!=0)
loadurl(this.hostname+"/user/adduser.php?mail="+val,"emailavilable");
}
/*
  Function Name: loginvalidate.
  Purpose      : Validation for Login.
  Developer    : Dhanaraj.
  Date         : 04-02-2010.

*/
function loginvalidate()
{
 var s=1;  
 var username=document.getElementById('username').value;
 var password=document.getElementById('password').value;
 if(username.length==0)
	{ 
		document.getElementById('unameerror').innerHTML="Enter Username";   
 		s=0;
	}
  if(password.length==0)
	{ 
		document.getElementById('passworderror').innerHTML="Enter Password";   
 		s=0;
	}
// form Submission Starts here..
	if(s==1)
	{  
		
		
		var encrypted;
		encrypted = hex_md5(document.login_form.password.value); // Ecrypting the password.
		 document.login_form.enc_password.value = encrypted;
               	document.login_form.action(this.hostname+"/user/userlogin.php");
               	document.login_form.submit();
               	
               
	}
// form Submission ends here..
}


function loginvalidate1()
{
 var s=1;  
 var username=document.getElementById('username1').value;
 var password=document.getElementById('password1').value;

 if(username.length==0)
	{ 
		document.getElementById('unameerror1').innerHTML="Enter Username";   
 		s=0;
	}

  if(password.length==0)
	{ 
		document.getElementById('passworderror1').innerHTML="Enter Password";   
 		s=0;
	}
// form Submission Starts here..

	if(s==1)
	{  
			
		var encrypted;
		encrypted = hex_md5(password); // Ecrypting the password.
		document.login_form1.enc_password1.value = encrypted;
        		document.login_form1.submit();
               
	}
// form Submission ends here..
}

/*
  Function Name: couponvalidate.
  Purpose      : Validation for Couponupload.
  Developer    : Dhanaraj.
  Date         : 05-02-2010.

*/
function couponvalidate()
{

 // Declarations
 c=1;
 var cname=document.getElementById('couponname').value;
 var cdesc=document.getElementById('cdesc').value;
 var cterms=document.getElementById('cterms').value;
 var cstartdate=document.getElementById('cstartdate').value;
 var cenddate=document.getElementById('cenddate').value;
 var minlimit=document.getElementById('minlimit').value;
 var maxlimit=document.getElementById('maxlimit').value;
 var coffer=document.getElementById('coffer').value;
 var crealvalue=document.getElementById('crealvalue').value;
 var ctype=document.getElementById('ctype').value;
 var illegalChars = /\W/;
 var cperson=document.getElementById('cperson').value;
 var cphoneno=document.getElementById('phonenum').value;
 var address=document.getElementById('address').value;
 var country=document.getElementById('country').value;

// var shop=document.getElementById('shop').value;

// Clear the innerHtml values
document.getElementById('cnameerror').innerHTML="";
document.getElementById('cdescerror').innerHTML="";
document.getElementById('ctermserror').innerHTML="";
document.getElementById('minlimiterror').innerHTML="";
document.getElementById('maxlimiterror').innerHTML="";
document.getElementById('limiterror').innerHTML="";
document.getElementById('cstartdateerror').innerHTML="";
document.getElementById('cenddateerror').innerHTML="";
document.getElementById('crealvalueerror').innerHTML="";
document.getElementById('ctypeerror').innerHTML="";

document.getElementById('cpersonerror').innerHTML="";
document.getElementById('cphonenoerror').innerHTML="";
document.getElementById('addresserror').innerHTML="";
document.getElementById('countryerror').innerHTML="";

if(document.getElementById('cityerror')!=null)
{
document.getElementById('cityerror').innerHTML="";
}
if(document.getElementById('subtypeerror')!=null)
{
document.getElementById('subtypeerror').innerHTML="";
}

if(document.getElementById('shoperror')!=null)
{
document.getElementById('shoperror').innerHTML="";
}

city="-1";
if(document.getElementById('city')!=null)
{
  city=document.getElementById('city').value;
}
csubtype="-1";
if(document.getElementById('csubtype')!=null)
{
  csubtype=document.getElementById('csubtype').value;
}
// Coupons Name Validation Starts here ...
  if(cname.length==0)
   {
    document.getElementById('cnameerror').innerHTML="Enter Coopon Name"; 
    c=0;
   }
  else if(cname.length > 50 || cname.length < 5)
   {
     document.getElementById('cnameerror').innerHTML="Coopon Name must 5 to 50 Characters";
     c=0;  
   }
// Coupons name Validation Ends here ...

// Coupons Description Validation Starts here ...
  if(cdesc.length==0)
   {
    document.getElementById('cdescerror').innerHTML="Enter Coopon Description";  
    c=0; 
   }
  else if(cdesc.length > 1000 || cdesc.length < 10)
   {
     document.getElementById('cdescerror').innerHTML="Coopon Description must 10 to 1000 Characters"; 
     c=0; 
   }
// Coupons Description Validation Ends here ...


// Coupons Terms Validation Starts here ...
  if(cterms.length==0)
   {
    document.getElementById('ctermserror').innerHTML="Enter Coopon using terms"; 
    c=0;  
   }
  else if(cterms.length > 1000 || cterms.length < 10)
   {
     document.getElementById('ctermserror').innerHTML="Coopon terms must 10 to 1000 Characters";  
     c=0;
   }
   

// Coupons terms Validation Ends here ...

// Coupons Limit Validation Starts here ...

  var stripped = minlimit.replace(/[\(\)\.\-\ ]/g, '');  
  
	if(minlimit.length==0)
	{ 
		document.getElementById('minlimiterror').innerHTML="Enter Coopon access minimun user Limit ";
                c=0;
	}
	else if (isNaN(parseInt(stripped)))
	{
		document.getElementById('minlimiterror').innerHTML="The Coopon access minimun user Limit contains illegal characters";
		c=0;
	}
	
  var stripped2 = maxlimit.replace(/[\(\)\.\-\ ]/g, '');  
	if(maxlimit.length==0)
	{ 
		document.getElementById('maxlimiterror').innerHTML="Enter Coopon access maximum user Limit ";
                c=0;
	}
	else if (isNaN(parseInt(stripped2)))
	{
		document.getElementById('maxlimiterror').innerHTML="The Coopon access maximum user Limit contains illegal characters";
		c=0;
	}

// check minimum user limit must be less than maximium user limit

	if(parseInt(document.getElementById('maxlimit').value)  <  parseInt(document.getElementById('minlimit').value))
	{
		document.getElementById('limiterror').innerHTML="The Coopon maximum user Limit should not be less than minimum user Limit";
		c = 0;	
	}
	
// Coupons Limit Validation Ends here ...

// Coupons Date Validation Starts here ...
var myDate=new Date(cstartdate);
var today = new Date();

if (myDate<today)
  {
  document.getElementById('cstartdateerror').innerHTML="Choose the Current date or greater than currentdate";
  c=0;
  }
var myDate=new Date(cenddate);
if (myDate<=today)
  {
  document.getElementById('cenddateerror').innerHTML="Choose the Date greater than Current date";
  c=0;
  }
var myDate=new Date(cstartdate);
var today = new Date(cenddate);
if (myDate>today)
  {
  document.getElementById('cenddateerror').innerHTML="Choose the Date greater than Start date ";
  c=0;
  }
// Coupons Date Validation Ends here ...

// Coupons offer Validation Starts here ...

  var stripped = coffer.replace(/[\(\)\.\-\ ]/g, '');  
	if(coffer.length==0)
	{ 
		document.getElementById('coffererror').innerHTML="Enter Coopon offer value Limit ";
                c=0;
	}
	else if (isNaN(parseInt(stripped)))
	{
		document.getElementById('coffererror').innerHTML="The Coopon offer value Limit contains illegal characters(Only Numbers)";
		c=0;
	}
	else if(coffer > 100)
	{
		document.getElementById('coffererror').innerHTML="The Coopon offer value Limit only up to 100 percent";
		c=0;	
	}
// Coupons offer Validation Ends here ...

  var stripped = crealvalue.replace(/[\(\)\.\-\ ]/g, '');  
	if(crealvalue.length==0)
	{ 
		document.getElementById('crealvalueerror').innerHTML="Enter Coopon Real Value";
                c=0;
	}
	else if (isNaN(parseInt(stripped)))
	{
		document.getElementById('crealvalueerror').innerHTML="The Coopon Real Value Limit contains illegal characters(Only Numbers)";
		c=0;
	}

if(ctype == "-2")
{
document.getElementById('ctypeerror').innerHTML="Choose Coopon Type";
c=0;
}


// Contact Person validation Starts here.

 	if(cperson.length==0)
	{ 
		document.getElementById('cpersonerror').innerHTML="Enter Contact Person Name";   
 		c=0;
	}
	else if(cperson.length > 20 || cperson.length < 5)
	{
  		document.getElementById('cpersonerror').innerHTML="Contact Person Name must 5 to 20 Characters";
		c=0;
	}
	else if (illegalChars.test(cperson))
	{
 		document.getElementById('cpersonerror').innerHTML="Special Characters not allowed.. [Numbers,Characters,underscore only Allowed]";
		c=0;
	}

// Contact Person validation ends here.

// Phone number validation Starts here.

	var stripped = cphoneno.replace(/[\(\)\.\-\ ]/g, '');  
	if(cphoneno.length==0)
	{ 
		document.getElementById('cphonenoerror').innerHTML="Enter Contact Phone no";
		c=0;
	}
	else if (isNaN(parseInt(stripped)))
	{
		document.getElementById('cphonenoerror').innerHTML="The phone number contains illegal characters";
		c=0;
	}
	else if (!(stripped.length == 10))
	{
   		 document.getElementById('cphonenoerror').innerHTML="The phone number is the wrong length. Make sure you included an area code";
		c=0;
	}
// Phone Number validation ends here.

// Address validation Starts here.

	if(address.length==0)
	{ 
		document.getElementById('addresserror').innerHTML="Enter Address";
		c=0;
	}
	else if(address.length > 100 || address.length < 6)
	{
		document.getElementById('addresserror').innerHTML="Address must 6 to 100 Characters";
		c=0;
	}

// Address validation ends here.

//Country and City validations
  	if(country=="-2")
            {
              document.getElementById('countryerror').innerHTML="Choose the Country..";
              c=0;
            }

	  if(city=="-2")
            {		
             document.getElementById('cityerror').innerHTML="Choose the City..";
             c=0;
	  }	
            
          if(csubtype=="-2")
            {		
             document.getElementById('subtypeerror').innerHTML="Choose the Subtype..";
             c=0;
	  }	

if(c==1)

	{
		 document.coupon_form.submit();
        }

}

function closecouponvalidate()
{ 
var val=document.getElementById('vid').value;
if(document.getElementById('vid').value!="")
loadurl(this.hostname+"/process/buycoupon.php?type=c&couponcode="+val,"offererror");
else
document.getElementById('offererror').innerHTML="Enter valid Id";
}

function getPid(val)
{
  document.getElementById('pid').value=val;
document.getElementById('offer').style.display='block';
}

function buycoupon(val)
{

loadurl(this.hostname+"/process/buycoupon.php?couponcode="+val,"purchasestatus");

}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}


function testloadcountry(val)
{
  if(val!='-2')
  { 
   if(document.getElementById('citytag')!=null)
	{
	document.getElementById('citytag').innerHTML=''; 
	 } 

       loadurl(this.hostname+"/user/loadstate.php?testcountrycode="+val,"dynamiclocation");
} 
}


function validateshop()
{

// Declaration 
 var s=1;
 var shopname=document.getElementById('shopname').value;
 var shopaddress=document.getElementById('shopaddress').value;
 var country=document.getElementById('country').value;
var lat = document.getElementById('lat').value;
var lang = document.getElementById('lang').value;

// Clear the innerHtml values
document.getElementById('shopnameerror').innerHTML="";
document.getElementById('shopaddresserror').innerHTML="";
document.getElementById('countryerror').innerHTML="";

	if(document.getElementById('cityerror')!=null)
	{
	document.getElementById('cityerror').innerHTML="";
	}


	if(document.getElementById('laterror')!=null)
	{
	document.getElementById('laterror').innerHTML="";
	}


	if(document.getElementById('langerror')!=null)
	{
	document.getElementById('langerror').innerHTML="";
	}

// Validations

	if(shopname=="" || shopname==null)
	{
	document.getElementById('shopnameerror').innerHTML="Choose the Shop Name..";
	s=0;
	}

	if(shopaddress=="" || shopaddress==null)
	{
	document.getElementById('shopaddresserror').innerHTML="Choose the Shop Address..";
	s=0;
	}

 	if(country=="-2")
            {
              document.getElementById('countryerror').innerHTML="Choose the Country..";
              s=0;
            }

if(country!="-2")
{ var city=document.getElementById('city').value;
            if(city=="-2")
            {
             document.getElementById('cityerror').innerHTML="Choose the City..";
             s=0;
            }
}

if(lat!="")
{ 
	if(lat=="" || lat==null || isNaN(lat)==true)
	{
             document.getElementById('laterror').innerHTML="Latitude value must be in numeric value..";
             s=0;
	}
	if(lang=="" || lang==null || isNaN(lang)==true)
	{
	         document.getElementById('langerror').innerHTML="Longitude value must be in numeric value..";
             s=0;
	}
}

if(lang!="")
{ 
	if(lat=="" || lat==null || isNaN(lat)==true)
	{
             document.getElementById('laterror').innerHTML="Latitude value must be in numeric value..";
             s=0;
	}
	if(lang=="" || lang==null || isNaN(lang)==true)
	{
	         document.getElementById('langerror').innerHTML="Longitude value must be in numeric value..";
             s=0;
	}
}
// form Submission Starts here..

	if(s==1)
	{ 
		document.form_createshop.submit();
		
	}
	
// form Submission ends here..
}

function validateeditshop()
{

// Declaration 
 var s=1;
 var shopname=document.getElementById('shopname').value;
 var shopaddress=document.getElementById('shopaddress').value;
 var country=document.getElementById('country').value;
var lat = document.getElementById('lat').value;
var lang = document.getElementById('lang').value;

// Clear the innerHtml values
document.getElementById('shopnameerror').innerHTML="";
document.getElementById('shopaddresserror').innerHTML="";
document.getElementById('countryerror').innerHTML="";

	if(document.getElementById('cityerror')!=null)
	{
	document.getElementById('cityerror').innerHTML="";
	}

	if(document.getElementById('laterror')!=null)
	{
	document.getElementById('laterror').innerHTML="";
	}

	if(document.getElementById('langerror')!=null)
	{
	document.getElementById('langerror').innerHTML="";
	}
	
	if(shopname=="" || shopname==null)
	{
	document.getElementById('shopnameerror').innerHTML="Choose the Shop Name..";
	s=0;
	}

	if(shopaddress=="" || shopaddress==null)
	{
	document.getElementById('shopaddresserror').innerHTML="Choose the Shop Address..";
	s=0;
	}

  	if(country=="-2")
            {
              document.getElementById('countryerror').innerHTML="Choose the Country..";
              s=0;
            }

	if(country!="-2")
           {

            var city = document.getElementById('city').value;
            if(city=="-2")
            {
            
             document.getElementById('cityerror').innerHTML="Choose the City..";
             s=0;
            }
            
	 }
	  

if(lat!="")
{ 
	if(lat=="" || lat==null || isNaN(lat)==true)
	{
             document.getElementById('laterror').innerHTML="Latitude value must be in numeric value..";
             s=0;
	}
	if(lang=="" || lang==null || isNaN(lang)==true)
	{
	         document.getElementById('langerror').innerHTML="Longitude value must be in numeric value..";
             s=0;
	}
}

if(lang!="")
{ 
	if(lat=="" || lat==null || isNaN(lat)==true)
	{
             document.getElementById('laterror').innerHTML="Latitude value must be in numeric value..";
             s=0;
	}
	if(lang=="" || lang==null || isNaN(lang)==true)
	{
	         document.getElementById('langerror').innerHTML="Longitude value must be in numeric value..";
             s=0;
	}
}

// form Submission Starts here..

	if(s==1)
	{ 
		document.form_editshop.submit();
		
	}
	
// form Submission ends here..
}

// Script for user edit

function usereditvalidate()
{

// Declaration 
 var s=1;var x=1;
 var type=document.getElementById('role').value;
 var firstname=document.getElementById('firstname').value;
 var lastname=document.getElementById('lastname').value;

 var email=document.getElementById('email').value;
 var emailavilable=document.getElementById('emailavilable').value;
 var mobile=document.getElementById('mobile').value;
 var address=document.getElementById('address').value;
 var country=document.getElementById('country').value;
 var illegalChars = /\W/;

// Clear the innerHtml values
document.getElementById('firstnameerror').innerHTML="";
document.getElementById('lastnameerror').innerHTML="";
document.getElementById('addresserror').innerHTML="";
document.getElementById('mobileerror').innerHTML="";
document.getElementById('countryerror').innerHTML="";
document.getElementById('emailerror').innerHTML="";

if(document.getElementById('cityerror')!=null)
{
document.getElementById('cityerror').innerHTML="";
}
if(document.getElementById('shoperror')!=null)
{
document.getElementById('shoperror').innerHTML="";
}
if(document.getElementById('ctypeerror')!=null)
{
document.getElementById('ctypeerror').innerHTML="";
}

// firstname and lastname validation start here

 	if(firstname.length==0)
	{ 
		document.getElementById('firstnameerror').innerHTML="Enter Firstname";   
 		s=0;
	}
	else if(firstname.length > 20 || firstname.length < 5)
	{
  		document.getElementById('firstnameerror').innerHTML="Firstname must 5 to 20 Characters";
		s=0;
	}
	else if (illegalChars.test(firstname))
	{
 		document.getElementById('firstnameerror').innerHTML="Special Characters not allowed.. [Numbers,Characters,underscore only Allowed]";
		s=0;
	}
	
 	if(lastname.length==0)
	{ 
		document.getElementById('lastnameerror').innerHTML="Enter Lastname";   
 		s=0;
	}
	else if(lastname.length > 20 || lastname.length < 5)
	{
  		document.getElementById('lastnameerror').innerHTML="Lastname must 5 to 20 Characters";
		s=0;
	}
		else if (illegalChars.test(lastname))
	{
 		document.getElementById('lastnameerror').innerHTML="Special Characters not allowed.. [Numbers,Characters,underscore only Allowed]";
		s=0;
	}	

// firstname and lastname validation end here

// Email validation Starts here.

	if(email.length==0)
	{ 
		document.getElementById('emailerror').innerHTML="Enter Email-Address";
		s=0;
	}
        else if (emailavilable=="Email Already Exists..")
          {
          	s=0;
	}
	else
	{
		var tfld = trim(email);                        // value of field with whitespace trimmed off
   		var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    		illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

  		if (!emailFilter.test(tfld)) 
 		{
 			 document.getElementById('emailerror').innerHTML="Enter Valid Email-Address";
			 s=0;
		}
 		else if (email.match(illegalChars))
 		{   
 			 document.getElementById('emailerror').innerHTML="The email address contains illegal characters";
			s=0;
		}
	}
// Email validation ends here.

// Address validation Starts here.

	if(address.length==0)
	{ 
		document.getElementById('addresserror').innerHTML="Enter Address";
		s=0;
	}
	else if(address.length > 100 || address.length < 6)
	{
		document.getElementById('addresserror').innerHTML="Address must 6 to 100 Characters";
		s=0;
	}

// Address validation ends here.


// Mobile number validation Starts here.

	var stripped = mobile.replace(/[\(\)\.\-\ ]/g, '');  
	if(mobile.length==0)
	{ 
		document.getElementById('mobileerror').innerHTML="Enter MobileNumber";
		s=0;
	}
	else if (isNaN(parseInt(stripped)))
	{
		document.getElementById('mobileerror').innerHTML="The phone number contains illegal characters";
		s=0;
	}
	else if (!(stripped.length == 10))
	{
   		 document.getElementById('mobileerror').innerHTML="The phone number is the wrong length. Make sure you included an area code";
		s=0;
	}
// Mobile Number validation ends here.

 	if(country=="-2")
            {
              document.getElementById('countryerror').innerHTML="Choose the Country..";
              s=0;x=0;
            }

// Based on the Role Validations Starts ...

        if(type=="CM")
        {
 
 if(document.getElementById('city')!=null) 
 {
           if(x==1)
           {

            var city=document.getElementById('city').value;
            if(city=="-2")
            {
             document.getElementById('cityerror').innerHTML="Choose the City..";
             s=0;
            }
          }
}
	}
	
        if(type=="CM")
        {
	if(city!="-2")
	{var shop=document.getElementById('shop').value;
	if(shop=="-2")
	{
             document.getElementById('shoperror').innerHTML="Choose the Shop Name..";
             s=0;	 
             }
	}        
        }	
	
       else if(type=="SA")
        {
 	   var shopname=document.getElementById('shopname').value;
           if(shopname=="-2")
            {
              document.getElementById('shoperror').innerHTML="Choose the Shop..";
              s=0;
            }
	}
       else if(type=="G")
        {
 	   var ctype=document.getElementById('ctype').value;
           if(ctype=="-2")
            {
              document.getElementById('ctypeerror').innerHTML="Choose the Coopon type..";
              s=0;
            }
	}	

// Based on the Role Validations Ends ...       

// form Submission Starts here..

	if(s==1)
	{ 
		
		document.userinformationedit_form.submit();
    		
	}
// form Submission ends here..
}

function Couponupdate(status,code)
{
   loadurl(this.hostname+'/process/updatecoupon.php?status='+status+'&couponcode='+code,'reportstatus');
}

function updateshop(status,code)
{
   loadurl(this.hostname+'/process/updateshop.php?status='+status+'&shopid='+code,'reportstatus');
}

function Updateuser(status,userid)
{
   loadurl(this.hostname+'/process/updateuser.php?status='+status+'&userid='+userid,'reportstatus');
}

function loadcooponcountry(val,type)
{

if(val!='-2')
  {
   if(document.getElementById('eccitytag')!=null)
	{
	document.getElementById('eccitytag').innerHTML=''; 
	 }  
	 
     if(type=="cm")
       loadurl(this.hostname+"/user/loadstate.php?loadcooponcountry="+val,"cdynamiclocation");
      else
	loadurl(this.hostname+"/user/loadstate.php?loadcooponcountry="+val+"&type=n","cdynamiclocation");
	
	}
}

function loadcooponcityshop(val)
{
  
	loadurl(this.hostname+"/user/loadstate.php?cooponcitycode="+val,"cityshopdetails");

}

function eusrloadcountry(val,type)
{
  if(val!='-2')
  { 
   if(document.getElementById('eusrcitytag')!=null)
	{
	document.getElementById('eusrcitytag').innerHTML=''; 
	 } 
     if(type=="cm")
       loadurl(this.hostname+"/user/loadstate.php?eusrcountrycode="+val,"eusrdynamiclocation");
 }
}


function changepasswordvalidate()
{
s=1;
 var password=document.getElementById('password').value;
 var repassword=document.getElementById('repassword').value;

document.getElementById('passworderror').innerHTML="";
document.getElementById('repassworderror').innerHTML="";

// Password validation Starts here..

	if(password.length==0)
	{ 
		document.getElementById('passworderror').innerHTML="Enter Password";
		s=0;
	}
	else if(password.length > 10 || password.length < 6)
	{
		 document.getElementById('passworderror').innerHTML="Password must 6 to 10 Characters";
		s=0;
	}

	if(repassword.length==0)
	{ 
		document.getElementById('repassworderror').innerHTML="Enter Re-Password";
		s=0;
	}
	else if(repassword!=trim(password))
	{  
		document.getElementById('repassworderror').innerHTML="Password doest not match";
		s=0;
	}
	
// Password validation ends here.
if(s==1)
{
		var encrypted;
		encrypted = hex_md5(document.changepassword_form.password.value); // Ecrypting the password.
		document.changepassword_form.enc_password.value = encrypted;
		document.changepassword_form.submit();
		}
}

function giftvalidate()
{

// Declaration 
 var s=1;
 var sname=document.getElementById('sname').value;
 var semail=document.getElementById('semail').value;
 var rname=document.getElementById('rname').value;
 var lastname=document.getElementById('remail').value;
 var remail=document.getElementById('remail').value;
 var cvid=document.getElementById('cvid').value;
 var illegalChars = /\W/;

// Clear the innerHtml values
document.getElementById('snameerror').innerHTML="";
document.getElementById('semailerror').innerHTML="";
document.getElementById('rnameerror').innerHTML="";
document.getElementById('remailerror').innerHTML="";
document.getElementById('cviderror').innerHTML="";

 	if(sname.length==0)
	{ 
		document.getElementById('snameerror').innerHTML="Enter SenderName";   
 		s=0;
	}
	else if(sname.length > 20 || sname.length < 5)
	{
  		document.getElementById('snameerror').innerHTML="SenderName must 5 to 20 Characters";
		s=0;
	}
	else if (illegalChars.test(sname))
	{
 		document.getElementById('snameerror').innerHTML="Special Characters not allowed.. [Numbers,Characters,underscore only Allowed]";
		s=0;
	}
// Username validation ends here..

// Email validation Starts here.

	if(semail.length==0)
	{ 
		document.getElementById('semailerror').innerHTML="Enter Email-Address";
		s=0;
	}
       
	else
	{
		var tfld = trim(semail);                        // value of field with whitespace trimmed off
   		var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    		illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

  		if (!emailFilter.test(tfld)) 
 		{
 			 document.getElementById('semailerror').innerHTML="Enter Valid Email-Address";
			 s=0;
		}
 		else if (semail.match(illegalChars))
 		{   
 			 document.getElementById('semailerror').innerHTML="The Email address contains illegal characters";
			s=0;
		}
	}
	

 	if(rname.length==0)
	{ 
		document.getElementById('rnameerror').innerHTML="Enter SenderName";   
 		s=0;
	}
	else if(rname.length > 20 || rname.length < 5)
	{
  		document.getElementById('rnameerror').innerHTML="SenderName must 5 to 20 Characters";
		s=0;
	}
	else if (illegalChars.test(rname))
	{
 		document.getElementById('rnameerror').innerHTML="Special Characters not allowed.. [Numbers,Characters,underscore only Allowed]";
		s=0;
	}
// Username validation ends here..

// Email validation Starts here.

	if(remail.length==0)
	{ 
		document.getElementById('remailerror').innerHTML="Enter Email-Address";
		s=0;
	}
       
	else
	{
		var tfld = trim(remail);                        // value of field with whitespace trimmed off
   		var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    		illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

  		if (!emailFilter.test(tfld)) 
 		{
 			 document.getElementById('remailerror').innerHTML="Enter Valid Email-Address";
			 s=0;
		}
 		else if (remail.match(illegalChars))
 		{   
 			 document.getElementById('remailerror').innerHTML="The Email address contains illegal characters";
			s=0;
		}
	}
	
	 	if(cvid.length==0)
		{ 
		document.getElementById('cviderror').innerHTML="Enter CooponValidityId";   
 		s=0;
		}
	
	
		if(s==1)
		{ 
		document.gift_form.submit();
		}
	
	}

