// JavaScript Document
var siteurl = "http://hantoor.com/";
function valid(str) {
        for(var i = 0; i < str.length; i++) {
            var charcode = str.charCodeAt(i);

            /* A-Z */
            if(charcode >= 0x41 && charcode <= 0x5A) {
                continue;
            }               
            /* a-z */
            if(charcode >= 0x61 && charcode <= 0x7A) {
                continue;
            }   
            /* 0-9 */
            if(charcode >= 0x30 && charcode <= 0x39) {
                continue;
            }               
            /* . - _ 32*/
            if(charcode == 0x2D || charcode == 0x2E || charcode == 0x5F  ) {
                continue;
            }
               
            return false;
        }//end for(var i = 0; i < str.length; i++)

        return true;

    }//end function valid
	
	function testemail(obj) {
        var atpos = obj.value.indexOf("@");
            if(atpos == -1) {
                return false;
            }

            if(atpos == 0) {
                return false;
            }

            var dotpos = obj.value.indexOf('.', atpos+2);

            if( dotpos == -1) {
                return false;
            }
               
            if(dotpos == (obj.value.length - 1) ) {
               return false;
            }
               
            if(obj.value.indexOf(".") == 0){
                return false;
            }
               
            if( obj.value.indexOf("@")  == (obj.value.indexOf('.', obj.value.indexOf("@")+1)) - 1 ){
                return false;
            }
                              
            if( obj.value.indexOf(".")  == (obj.value.indexOf('.', obj.value.indexOf(".")+1)) - 1 ){
	        return false;
            }
               
                             
           var fpart = obj.value.substring(0,atpos);
           var host = obj.value.substring(atpos + 1, dotpos);
           var domain = obj.value.substr(dotpos +1);

           if(!( valid(fpart) && valid(host) && valid(domain))) {
               return false;
           }
     
           var afterat = obj.value.substr(atpos + 1);
     
           if(afterat.lastIndexOf('.') == (afterat.length - 1)) {
               return false;
           }
     
           for(var i = 1; i < afterat.length; i++) {
        
     	       if(afterat.charAt(i) == '.' && afterat.charAt(i-1) == '.') {
                   return false;
               }

           }//end for(var i = 1; i < afterat.length; i++)

           return true;
                  
    }//end function testemail
    	
    function trim(string) {
		return string.replace(/^\s+|\s+$/, '');
	}
    
	function goSubscription(){
		document.subscriptionForm.email.value = trim(document.subscriptionForm.email.value);
		if(document.subscriptionForm.email.value == ""){
		
    		alert("بريدك الالكتروني فارغ");
    	
	 		document.subscriptionForm.email.focus();
	 		return false;
    	}else if (!testemail(document.subscriptionForm.email) ){
    	
            alert("عذراً,الرجاء ادخال بريد إلكتروني صحيحً");
        
            document.subscriptionForm.email.focus();
            return false;
    	}else{
    		document.subscriptionForm.submit();
    	}
	}
		
		function getType(categoryId,type,typeSelected){
				if (typeSelected == null){
				   typeSelected = 0;
				}
				$.ajax({
				type: "get",
				url:  siteurl + "getType.php",
				data: "categoryId="+categoryId+"&type="+type+"&typeSelected="+typeSelected,
				success: getMatchesResponseType,
				beforeSend: function() { jQuery('#type').html("Loading....")
				                        jQuery('#type1').html("Loading....")}
				});
			}
			
			function getMatchesResponseType(response){
				$('#type').html(response);
				$('#type1').html(response);
			}	
	
		function getSubTypeSerch(typeId,type){
			$.ajax({
			type: "get",
			url:  siteurl + "getSubType.php",
			data: "typeId="+typeId+'&type='+type,
			success: getMatchesResponseSubTypeSerch,
			beforeSend: function() { jQuery('#carSubTypeSearch').html("Loading....")
									jQuery('#carSubTypeSearch1').html("Loading....")}
			});
			}
		
		function getMatchesResponseSubTypeSerch(response){
			$('#carSubTypeSearch').html(response);
			$('#carSubTypeSearch1').html(response);
		}
		
		
		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=375,height=189,left = 584.5,top = 369');");
		}
