// LTrim(string) : Returns a copy of a string without leading spaces.
function ltrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

//RTrim(string) : Returns a copy of a string without trailing spaces.
function rtrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}

// Trim(string) : Returns a copy of a string without leading or trailing spaces
function trim(str) {
   return rtrim(ltrim(str));
}
/**
 * Verifies if the string is in a valid email format
 * @param	string
 * @return	boolean
 */
function isEmail( text )
{
	var pattern = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex = new RegExp( pattern );
	return regex.test( text );
}

function sendContact(url)
{
    Dialog.info("Please wait...",{width:200, height:100, showProgress: true}); 
    var data = $('frm_contact').serialize();
    Form.reset('frm_contact');
               new Ajax.Request(url, {
					  method: 'post',
					  parameters:data,
					  onSuccess: function(transport) {
                                      Dialog.closeInfo();
                      Dialog.alert(transport.responseText, {className: "alphacube", width:350, okLabel: "Close",ok:function(win){Dialog.closeInfo();}});  
                       setTimeout(hideDialog, 9000) 
                  }
                   
           });      
  
}
function hideDialog()
{
	Dialog.closeInfo();
}

function createAccount(url)
{
    Dialog.info("Please wait...", {width:200, height:100, showProgress: true}); 
    var data = $('frm_account').serialize();
   Form.reset('frm_account');
               new Ajax.Request(url, {
					  method: 'post',
					  parameters:data,
					  onSuccess: function(transport) {
                                      Dialog.closeInfo();
                      Dialog.alert(transport.responseText, {className: "alphacube", width:350, okLabel: "Close",ok:function(win){Dialog.closeInfo();}});  
                     setTimeout(hideDialog, 9000) 
                  }
                   
           });      
}

 function recoverPassword(url)
 {
	setVisibility('forgot-password', 'none');
	Dialog.info("Please wait...", {width:200, height:100, showProgress: true}); 
    var data = $('frm_recover').serialize();
    Form.reset('frm_recover');
               new Ajax.Request(url, {
					  method: 'post',
					  parameters:data,
					  onSuccess: function(transport) {
                                      Dialog.closeInfo();
                      Dialog.alert(transport.responseText, {className: "alphacube", width:350, okLabel: "Close",ok:function(win){Dialog.closeInfo();}});  
                     setTimeout(hideDialog, 9000) 
                  }
                   
           });      
 }


//script included in template

function loginClient() //login template
 {
    var valid = new Validation('frm_login', {useTitles : true},{onSubmit : false});
    var result = valid.validate();
     if(result == true )
     {
           $('frm_login').submit();
     } 
 }

 function submitAccountInfo(url) //signup template
 {
    var valid = new Validation('frm_account', {useTitles : true},{onSubmit : false});
    var result = valid.validate();
     if(result == true )
     {
         if($('aggreement').checked == true)
		 {
            createAccount(url);
		 }
		 else
		 {
            alert('Please select agree terms and privacy policy');
			return false;
		 }
     } 
 }

 //contact us 
 function submitContact(url)
 {
	$('help').focus();
    var valid = new Validation('frm_contact', {useTitles : true},{onSubmit : false});
    var result = valid.validate();
     if(result == true )
     {
           sendContact(url);
     } 
 }

 
 function loginClient2(url,form) //login template
 {	
	 
    var valid = new Validation(form, {useTitles : true},{onSubmit : false});
    var result = valid.validate();
	//alert( $(form).sub_domain.value);return false;
     if(result == true )
     {
         var domain = $(form).sub_domain.value+"."+$(form).domainName.value;
	   if(checkDomain(domain))
		 {
		   Dialog.info("Please wait...", {width:200, height:100, showProgress: true}); 
		  	   var data = $(form).serialize();
			        new Ajax.Request(url, {
					  method: 'post',
					  parameters:data,
					  onSuccess: function(transport) {
						
						  if(transport.responseText == "success")
						  {
								Dialog.closeInfo();
								$(form).action = "http://"+domain+"/Login.aspx";
								$(form).submit();
						  }
						  else
						  {
							  Dialog.closeInfo();
							  Dialog.alert("Account does not exist!&nbsp; Please enter correct account name.", {className: "alphacube", width:350, okLabel: "Close",ok:function(win){Dialog.closeInfo();}});  
						  }
                  }
                   
           }); 
		 } 
	 }
 }


function checkDomain(nname)
{
var arr = new Array(
'.com','.net','.org','.biz','.coop','.info','.museum','.name',
'.pro','.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag',
'.ai','.al','.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw',
'.az','.ba','.bb','.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm',
'.bn','.bo','.br','.bs','.bt','.bv','.bw','.by','.bz','.ca','.cc',
'.cd','.cf','.cg','.ch','.ci','.ck','.cl','.cm','.cn','.co','.cr',
'.cu','.cv','.cx','.cy','.cz','.de','.dj','.dk','.dm','.do','.dz',
'.ec','.ee','.eg','.eh','.er','.es','.et','.fi','.fj','.fk','.fm',
'.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh','.gi','.gl','.gm',
'.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gv','.gy','.hk','.hm',
'.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io','.iq',
'.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki',
'.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li',
'.lk','.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.mg',
'.mh','.mk','.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt',
'.mu','.mv','.mw','.mx','.my','.mz','.na','.nc','.ne','.nf','.ng',
'.ni','.nl','.no','.np','.nr','.nu','.nz','.om','.pa','.pe','.pf',
'.pg','.ph','.pk','.pl','.pm','.pn','.pr','.ps','.pt','.pw','.py',
'.qa','.re','.ro','.rw','.ru','.sa','.sb','.sc','.sd','.se','.sg',
'.sh','.si','.sj','.sk','.sl','.sm','.sn','.so','.sr','.st','.sv',
'.sy','.sz','.tc','.td','.tf','.tg','.th','.tj','.tk','.tm','.tn',
'.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug','.uk','.um',
'.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu','.ws',
'.wf','.ye','.yt','.yu','.za','.zm','.zw');

var mai = nname;
var val = true;

var dot = mai.lastIndexOf(".");
var dname = mai.substring(0,dot);
var ext = mai.substring(dot,mai.length);
//alert(ext);
	
if(dot>2 && dot<57)
{
	for(var i=0; i<arr.length; i++)
	{
	  if(ext == arr[i])
	  {
	 	val = true;
		break;
	  }	
	  else
	  {
	 	val = false;
	  }
	}
	if(val == false)
	{
	  	 alert("Your domain extension "+ext+" is not correct");
		 return false;
	}
	else
	{
		for(var j=0; j<dname.length; j++)
		{
		  var dh = dname.charAt(j);
		  var hh = dh.charCodeAt(0);
		  if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || hh==45 || hh==46)
		  {
			 if((j==0 || j==dname.length-1) && hh == 45)	
		  	 {
		 	  	 alert("Domain name should not begin are end with '-'");
			      return false;
		 	 }
		  }
		else	{
		  	 alert("Your domain name should not have special characters");
			 return false;
		  }
		}
	}
}
else
{
 alert("Your Domain name is too short/long");
 return false;
}	

return true;
}
