/**
 *
 * 
 *
 */
 
 
 function checkNewCountry(){
	var f  = document.new_country_frm;
	var en = f.co_name_en;
	var an = f.co_name_ar;
	var cc = f.co_code;
	var ec = f.cur_name;
	var ac = f.cur_name_ar;
	var gmt= f.co_zone_gmt;
	
	var custum_erro = false;
	for(var i=0; i <= f.length-1; i++){		
	
	  	if(f[i].name == "") continue;
		
		if(!document.getElementById('error_' + f[i].name)){
		  if(f[i].value == ''){
			   $(f[i]).addClass("r_error");
			   custum_erro = true;
		  }else{
			  $(f[i]).removeClass("r_error");
		  }
		}
	}
	if(custum_erro){
		$("div#m_error").html('<div class="error">You Have error(s) in your form !</div>');
		return false;
	}
	f.submit();
 }
 
 function checkCity(){
	var f = document.cities_frm;
	for(var i=0; i <= f.length-1; i++){		
	  	if(f[i].name == "") continue;
		if(!document.getElementById('error_' + f[i].name)){
		  if(f[i].value == ''){
			   $(f[i]).addClass("r_error");
			   return false;
		  }else{
			  $(f[i]).removeClass("r_error");
		  }
		}
	}
	return true;
 }
 
 function checkFrm(f){
	for(var i=0; i <= f.length-1; i++){
		/** 
		 * when You want to step out loop
		 */
		 //alert(f[i].type);
		if(f[i].type == "hidden") continue; // step out hidden elements
		if(f[i].type == "file") continue;   // step out file elements
		if(f[i].type == "select-one") continue;   // step out file elements
		if(f[i].type == 'checkbox') continue; 
	  	if(f[i].name == "") continue;       // step out not named elements
		if(f[i].getAttribute('require') == 'false') continue; // step out elements with require='false' attribute 
		
		if(!document.getElementById('error_' + f[i].name)){
			if(f[i].alt == 'number' && !IsNumeric(f[i].value)){
				  var elementX = findPosX(f[i]);
				  var elementY = findPosY(f[i]) - 35;
				  elementY     = parseInt(elementY);	
				  $(f[i]).addClass("r_error");			  
				    __showError('Should be number');				  										
				  
				  return false;
			  }else{
				 $("div#numbers_div").remove();  
				 $(f[i]).removeClass("r_error");
			  }
			  
			  if(f[i].alt == 'email' && !echeck(f[i].value)){
			   var elementX = findPosX(f[i]);
				  var elementY = findPosY(f[i]) - 35;
				  elementY     = parseInt(elementY);				 
				    __showError('Should be valid E-mail');
				   f[i].focus();				 
				  return false;
			  }else{
				 $("div#numbers_div").remove();  
			  }
		  if(f[i].value == '' || f[i].value == 'First Name, Last Name'){			  
			   $(f[i]).addClass("r_error");
			   //f[i].focus();
			   $.scrollTo( f[i], 900 ,{offset:-50});
			   __showError('Please fill all fields with stars (*)');
			   return false;
		  }else{
			  $(f[i]).removeClass("r_error");
		  }
		}
	}

	   var email = f.email;
		var conemail = f.con_email;
		var agree = f.agree;
		if(email.value !== conemail.value){
		 $.scrollTo( email, 900 ,{offset:-50});
		 $(email).addClass('r_error');
		 __showError("Please set valid Two emails");
		 return false;
	  }else{
		 $(email).removeClass('r_error');
	  }
	  if(agree.checked == false){
	   $.scrollTo( agree, 900 ,{offset:-50});
		 $(agree).parent().addClass('r_error');
		 __showError("You should agree with <a href='./terms' target='_blank'>terms and conditions</a>");
		 return false;
	  }

	  var js_date = new Date();
	  var cc2_month = js_date.getMonth();
	  var cc2_year  = js_date.getFullYear();
	  if(f.cc_year.value == cc2_year){
	  	if(parseInt(f.cc_month.value) < cc2_month){
	  	  __showError("Credit card expiry date is so late !");
	  	  return false;
	  	}
	  }
	
	if(check_cc(f)){
		f.submit();
	}
 }
 
 
 function __showError(er){
  if(!document.getElementById('error_msg')){
	 	$("body").append("<div id='#error_msg' class='error_top'>"+er+"</div>");
	}else{
	  $("#error_msg").html(er);
	}
 	$(".error_top").slideDown();
 	$(".error_top").animate({ backgroundColor: "#EF5959;" }, "slow");
 	setTimeout("$('.error_top').slideUp(1000);",5000);
 }
 
 // check form in book.php page for accept terms of use
  
  function reCheckFrm(f){
		
  }
  
  // check credit card in book.php page  
  function check_cc(f){
   var number = f.cc_number.value;   
   // get value of cc's type radio bottons
   for (var i=0; i < f.cc_type.length; i++){		
		if (f.cc_type[i].checked){
		   var type = f.cc_type[i].value;
		   }
	 }		 
	if (!checkCreditCard (number, type)){
	   $(f.cc_number).addClass('r_error');
	   $(f.cc_type).addClass('r_error');	   
		__showError('Credit card number is in invalid format');
		return false;
 	}else{
 	   $(f.cc_number).removeClass('r_error');
	   $(f.cc_type).removeClass('r_error');
	   return true;
 	}
  }
 
   
 function IsNumeric(sText){
   var ValidChars = "0123456789.-_()";
   var IsNumber = true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;   
   }

 
 
 function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

 
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		  
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		   
		    return false
		 }

 		 return true					
	}

 function cardval(s) {
	// remove non-numerics
	var v = "0123456789";
	var w = "";
	for (i=0; i < s.length; i++) {
	x = s.charAt(i);
	if (v.indexOf(x,0) != -1)
	w += x;
	}
	// validate number
	j = w.length / 2;
	if (j < 6.5 || j > 8 || j == 7) return false;
	k = Math.floor(j);
	m = Math.ceil(j) - k;
	c = 0;
	for (i=0; i<k; i++) {
	a = w.charAt(i*2+m) * 2;
	c += a > 9 ? Math.floor(a/10 + a%10) : a;
	}
	for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
	return (c%10 == 0);
}

 function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=520,height=320,left = 390,top = 150');");
 }
 function strtotime(d1, d2) {
    var date1 = new Date(d1);
	var date2 = new Date(d2);
	var sec = date2.getTime() - date1.getTime();
	if (isNaN(sec))
	{
		alert("Input data is incorrect!");
		return;
	}
	if (sec < 0)
	{
		alert("The second date ocurred earlier than the first one!");
		return;
	}
 
	var second = 1000, minute = 60 * second, hour = 60 * minute, day = 24 * hour;
 
	
	var days = Math.floor(sec / day);
	sec -= days * day;
	var hours = Math.floor(sec / hour);
	sec -= hours * hour;
	var minutes = Math.floor(sec / minute);
	sec -= minutes * minute;
	var seconds = Math.floor(sec / second);
	return days;
}


 function checkFrm3(f){
	for(var i=0; i <= f.length-1; i++){
		/** 
		 * when You want to step out loop
		 */
		if(f[i].type == "hidden") continue; // step out hidden elements
		if(f[i].type == "file") continue;   // step out file elements
	  	if(f[i].name == "") continue;       // step out not named elements
		if(f[i].getAttribute('require') == 'false') continue; // step out elements with require='false' attribute 
		
		if(!document.getElementById('error_' + f[i].name)){
			if(f[i].alt == 'number' && !IsNumeric(f[i].value)){
				  var elementX = findPosX(f[i]);
				  var elementY = findPosY(f[i]) - 35;
				  elementY     = parseInt(elementY);
				  if(!document.getElementById('numbers_div')){
				    $("body").append("<div class='numbers_error' id='numbers_div' style=' top: " + elementY + "px; left: " + elementX + "px; position: absolute; '>Should be number</div>");
				  }else{
					$("div#numbers_div").css("top",elementY);
					$("div#numbers_div").css("left",elementX);
				  }
				  return false;
			  }else{
				 $("div#numbers_div").remove();  
			  }
			  
			  if(f[i].alt == 'email' && !echeck(f[i].value)){
			   var elementX = findPosX(f[i]);
				  var elementY = findPosY(f[i]) - 35;
				  elementY     = parseInt(elementY);
				  if(!document.getElementById('numbers_div')){
				    $("body").append("<div class='numbers_error' id='numbers_div' style=' top: " + elementY + "px; left: " + elementX + "px; position: absolute; '>Should be valid E-mail</div>");
				   f[i].focus();
				  }else{
					$("div#numbers_div").css("top",elementY);
					$("div#numbers_div").css("left",elementX);
				  }
				  return false;
			  }else{
				 $("div#numbers_div").remove();  
			  }
		  if(f[i].value == ''){
			  
			   $(f[i]).addClass("r_error");
			   f[i].focus();
			   if(!document.getElementById('error')) $(f).before("<div id='error' class='error'>You have error(s) in the form</div>");
			   return false;
		  }else{
			  $(f[i]).removeClass("r_error");
		  }
		}
	}
	f.submit();
 }
 
 
 

