function Validate_Input(res){
	if(res.length==0) return false;
	return true;
}
function Validate_EmailInput(txt){
	if(txt.length==0){ 
	  return false; 
	}else{
		var str = txt;
		var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; 
		var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
		if (!reg1.test(str) && reg2.test(str)) {
			return true;
		}else{
			return false; 
		}
	  return false;
	}
}
function ShowHideLeftMenu(id,count){
	if(document.getElementById('left_menu_'+id).style.display==''){
		var d=1;
	}else{
		var d=0;
	}
	for(var i=0;i<count;i++){
		if(document.getElementById('left_menu_'+i)) document.getElementById('left_menu_'+i).style.display='none';
		if(document.getElementById('img_minus_'+i))document.getElementById('img_minus_'+i).style.display='none';
		if(document.getElementById('img_plus_'+i)) document.getElementById('img_plus_'+i).style.display='';
	}
	if(d==1){
		document.getElementById('left_menu_'+id).style.display='none';
		document.getElementById('img_plus_'+id).style.display='';
		document.getElementById('img_minus_'+id).style.display='none';
	}else{
		document.getElementById('left_menu_'+id).style.display='';
		document.getElementById('img_plus_'+id).style.display='none';
		document.getElementById('img_minus_'+id).style.display='';
	}
}
function ShowHideFAQ(id,count){
	for(var i=0;i<count;i++){
		if(document.getElementById('faq_'+i)) document.getElementById('faq_'+i).style.display='none';
	}
	document.getElementById('faq_'+id).style.display='';
}
function ProductChangeImage(id){
	x_Shop_GetProductImage(id,ProductChangeImage_Display);
}
function ProductChangeImage_Display(res){
	if(res==false){
		
	}else{
		document.getElementById("image_detail").innerHTML=res;
	}
}
//////////////////////cart///////////////////
function Cart_AddToBasket(product_id,qty,size){
	if(!product_id || !qty) return false;
	x_cart_AddToCart(product_id,qty,size,Cart_AddToBasket_Res);
}
function Cart_AddToBasket_Res(res){
	x_shop_ShopingCartDisplay(Cart_AddToBasket_Display);
}
function Cart_AddToBasket_Display(res){
	document.getElementById("shopping_cart").innerHTML=res;
}
function Cart_DeleteFromBasket(product_id,size){
	x_cart_DeleteFromCart(product_id,size,Cart_AddToBasket_Res);
}
function check_mode(NForm){
	if(NForm.mode.value.length==0){
		return false;
	}
	return true;
}
function Update_basket(){
	document.getElementById("mode").value="update";
	document.getElementById("cart_form").submit();
}
function Deletefrom_basket(){
	document.getElementById("mode").value="delete";
	document.getElementById("cart_form").submit();
}
function Clear_basket(){
	document.getElementById("mode").value="clear";
	document.getElementById("cart_form").submit();
}
function Save_basket(){
	x_LoginUserCheckLogin(Save_basket_res);
}
function Save_basket_res(res){
	if(res==true){
		document.getElementById("mode").value="save";
		document.getElementById("cart_form").submit();
	}else{
		alert("Please Login First");
		return false;
	}
}
function Pay_basket(){
	x_LoginUserCheckLogin(Pay_basket_res);
}
function Pay_basket_res(res){
	if(res==true){
		document.getElementById("mode").value="pay";
		document.getElementById("cart_form").submit();
	}else{
		alert("Please Login First");
		return false;
	}
}

function alphaNumericCheck(value){
	if(value == ''){
		return false;
	}
	
	var regex=/^[0-9A-Za-z]+$/; //^[a-zA-z]+$/
	if(regex.test(value)){
		return true;
	}
	else {
		return false;
	}
}

function removeAllItems(selectbox){if(selectbox==null) return;for(var i=selectbox.options.length-1;i>=0;i--){selectbox.remove(i);}}

function remove_table_row(row_id){
	var row = document.getElementById(row_id);
	row.parentNode.removeChild(row);
}

//glue to convert between options string
//and option objects
//used becasue of IE 'select.innerHTML' bug 
function select_options(select_id,options_string){
	
	var select_obj=document.getElementById(select_id);
	
	if( (typeof select_obj == 'undefined') || (select_obj.nodeName != 'SELECT') ){
		return false;
	}
	
	removeAllItems(select_obj);
	
	var options = options_string.split('</option>');
	var i = 0;
	var option_value;
	var option_name;
	var option_selected;
	var split_option;
	
	while(i < options.length){
		
		//is valid?
		if(options[i].length < 2){
			i++;
			continue;
		}
		
		//selected?
		option_selected = false;
		if( options[i].indexOf('selected') != -1 ){
			option_selected = true;
		}
		
		//name
		split_option =  options[i].split('>');
		option_name = split_option[split_option.length-1];
		
		//value
		option_value = '';
		if( options[i].indexOf('value="') != -1){
			option_value = options[i].substring(options[i].indexOf('value="')+7,options[i].length);
			option_value = option_value.split('"');
			option_value = option_value[0];
		}
		
		//add to select
		var new_option = document.createElement('option');
		
		new_option.innerHTML=option_name;
		new_option.value=option_value;
		new_option.selected=option_selected;
		
		select_obj.appendChild(new_option);
		
		i++;
	}
}

function copy_address_old(){
	
	if(document.getElementById("check1").checked==true){
		document.getElementById("d_fname").value=document.getElementById("fname").value;
		document.getElementById("d_lname").value=document.getElementById("lname").value;
		document.getElementById("d_phone").value=document.getElementById("phone").value;
		document.getElementById("d_country").value=document.getElementById("country").value;
		document.getElementById("d_address").value=document.getElementById("baddress").value;
		document.getElementById("d_city").value=document.getElementById("city").value;
		document.getElementById("d_postal_code").value=document.getElementById("postal_code").value;
		
	}
	else{
		document.getElementById("d_fname").value='';
		document.getElementById("d_lname").value='';
		document.getElementById("d_phone").value='';
		document.getElementById("d_country").value='';
		document.getElementById("d_address").value='';
		document.getElementById("d_city").value='';
		document.getElementById("d_postal_code").value='';
	}
}

function Validate_AddCustomer(){
	document.getElementById("td_err").innerHTML='&nbsp;';
	document.getElementById("td_f_name").style.color='';
	document.getElementById("td_m_name").style.color='';
	document.getElementById("td_l_name").style.color='';
	document.getElementById("td_home").style.color='';
	document.getElementById("td_mobile").style.color='';
	document.getElementById("td_email").style.color='';
	document.getElementById("td_country").style.color='';
	document.getElementById("td_region").style.color='';
	document.getElementById("td_area").style.color='';
	document.getElementById("td_street").style.color='';
	document.getElementById("td_building").style.color='';
	var f_name=document.getElementById("f_name").value;
	var m_name=document.getElementById("m_name").value;
	var l_name=document.getElementById("l_name").value;
	var home=document.getElementById("home").value;
	var mobile=document.getElementById("mobile").value;
	var email=document.getElementById("email").value;
	var country=document.getElementById("cus_country").value;
	var region=document.getElementById("region").value;
	var area=document.getElementById("area").value;
	var street=document.getElementById("street").value;
	var building=document.getElementById("building").value;
	var x=0;
	if(!Validate_Input(f_name)){
		document.getElementById("td_f_name").style.color='red';
		x=1;
	}
	if(!Validate_Input(m_name)){
		document.getElementById("td_m_name").style.color='red';
		x=1;
	}
	if(!Validate_Input(l_name)){
		document.getElementById("td_l_name").style.color='red';
		x=1;
	}
	if(!Validate_Input(home) && !Validate_Input(mobile)){
		document.getElementById("td_home").style.color='red';
		document.getElementById("td_mobile").style.color='red';
		x=1;
	}
	/*if(!Validate_EmailInput(email)){
		document.getElementById("td_email").style.color='red';
		x=1;
	}*/
	if(!Validate_Input(country)){
		document.getElementById("td_country").style.color='red';
		x=1;
	}
	if(!Validate_Input(region)){
		document.getElementById("td_region").style.color='red';
		x=1;
	}
	if(!Validate_Input(area)){
		document.getElementById("td_area").style.color='red';
		x=1;
	}
	if(!Validate_Input(street)){
		document.getElementById("td_street").style.color='red';
		x=1;
	}
	if(!Validate_Input(building)){
		document.getElementById("td_building").style.color='red';
		x=1;
	}
	if(x==1){
		document.getElementById("td_err").innerHTML="<b>Please Fill Missing Fileds in red</b>";
		return false;
	}
	return true;
}


function copy_address(){
	
	if(document.getElementById("check1").checked==true){
		
		document.getElementById("bf_name").value=document.getElementById("f_name").value;
		document.getElementById("bm_name").value=document.getElementById("m_name").value;
		document.getElementById("bl_name").value=document.getElementById("l_name").value;
		document.getElementById("bphone").value=document.getElementById("phone").value;
		document.getElementById("bmobile").value=document.getElementById("mobile").value;
		document.getElementById("bemail").value=document.getElementById("email").value;
		document.getElementById("bcountry").value=document.getElementById("country").value;
		get_regions3(document.getElementById("country").value);
		get_areas3(document.getElementById("region").value);
		document.getElementById("bstreet").value=document.getElementById("street").value;
		document.getElementById("bbuilding").value=document.getElementById("building").value;
		
	}
	else{
		document.getElementById("bf_name").value='';
		document.getElementById("bm_name").value='';
		document.getElementById("bl_name").value='';
		document.getElementById("bphone").value='';
		document.getElementById("bmobile").value='';
		document.getElementById("bemail").value='';
		document.getElementById("bcountry").value='';
		document.getElementById("bregion").value='';
		document.getElementById("barea").value='';
		document.getElementById("bstreet").value='';
		document.getElementById("bbuilding").value='';
	}
}
function Validate_DeliveryForm(){
	document.getElementById("td_err").innerHTML='&nbsp;';
	document.getElementById("td_bf_name").style.color='';
	document.getElementById("td_f_name").style.color='';
	document.getElementById("td_bm_name").style.color='';
	document.getElementById("td_m_name").style.color='';
	document.getElementById("td_bl_name").style.color='';
	document.getElementById("td_l_name").style.color='';
	document.getElementById("td_bphone").style.color='';
	document.getElementById("td_phone").style.color='';
	document.getElementById("td_bmobile").style.color='';
	document.getElementById("td_mobile").style.color='';
	document.getElementById("td_bemail").style.color='';
	document.getElementById("td_email").style.color='';
	document.getElementById("td_bcountry").style.color='';
	document.getElementById("td_country").style.color='';
	document.getElementById("td_bregion").style.color='';
	document.getElementById("td_region").style.color='';
	document.getElementById("td_barea").style.color='';
	document.getElementById("td_area").style.color='';
	document.getElementById("td_bstreet").style.color='';
	document.getElementById("td_street").style.color='';
	document.getElementById("td_bbuilding").style.color='';
	document.getElementById("td_building").style.color='';
	
	var bf_name=document.getElementById("bf_name").value;
	var f_name=document.getElementById("f_name").value;
	var bm_name=document.getElementById("bm_name").value;
	var m_name=document.getElementById("m_name").value;
	var bl_name=document.getElementById("bl_name").value;
	var l_name=document.getElementById("l_name").value;
	var bphone=document.getElementById("bphone").value;
	var phone=document.getElementById("phone").value;
	var bmobile=document.getElementById("bmobile").value;
	var mobile=document.getElementById("mobile").value;
	var bemail=document.getElementById("bemail").value;
	var email=document.getElementById("email").value;
	var bcountry=document.getElementById("bcountry").value;
	var country=document.getElementById("country").value;
	var bregion=document.getElementById("bregion").value;
	var region=document.getElementById("region").value;
	var barea=document.getElementById("barea").value;
	var area=document.getElementById("area").value;
	var bstreet=document.getElementById("bstreet").value;
	var street=document.getElementById("street").value;
	var bbuilding=document.getElementById("bbuilding").value;
	var building=document.getElementById("building").value;
	var x=0;
	if(!Validate_Input(bf_name)){
		document.getElementById("td_bf_name").style.color='red';
		x=1;
	}
	if(!Validate_Input(f_name)){
		document.getElementById("td_f_name").style.color='red';
		x=1;
	}
	if(!Validate_Input(bm_name)){
		document.getElementById("td_bm_name").style.color='red';
		x=1;
	}
	if(!Validate_Input(m_name)){
		document.getElementById("td_m_name").style.color='red';
		x=1;
	}
	if(!Validate_Input(bl_name)){
		document.getElementById("td_bl_name").style.color='red';
		x=1;
	}
	if(!Validate_Input(l_name)){
		document.getElementById("td_l_name").style.color='red';
		x=1;
	}
	if(!Validate_Input(bphone) && !Validate_Input(bmobile)){
		document.getElementById("td_bphone").style.color='red';
		document.getElementById("td_bmobile").style.color='red';
		x=1;
	}
	if(!Validate_Input(phone) && !Validate_Input(mobile)){
		document.getElementById("td_phone").style.color='red';
		document.getElementById("td_mobile").style.color='red';
		x=1;
	}
	/*if(!Validate_Input(bemail)){
		document.getElementById("td_bemail").style.color='red';
		x=1;
	}*/
	/*if(!Validate_Input(email)){
		document.getElementById("td_email").style.color='red';
		x=1;
	}*/
	if(!Validate_Input(country)){
		document.getElementById("td_country").style.color='red';
		x=1;
	}
	if(!Validate_Input(region)){
		document.getElementById("td_region").style.color='red';
		x=1;
	}
	if(!Validate_Input(area)){
		document.getElementById("td_area").style.color='red';
		x=1;
	}
	
	if(!Validate_Input(bcountry)){
		document.getElementById("td_bcountry").style.color='red';
		x=1;
	}
	if(!Validate_Input(bregion)){
		document.getElementById("td_bregion").style.color='red';
		x=1;
	}
	if(!Validate_Input(barea)){
		document.getElementById("td_barea").style.color='red';
		x=1;
	}
	if(!Validate_Input(area)){
		document.getElementById("td_area").style.color='red';
		x=1;
	}
	if(!Validate_Input(bstreet)){
		document.getElementById("td_bstreet").style.color='red';
		x=1;
	}
	if(!Validate_Input(street)){
		document.getElementById("td_street").style.color='red';
		x=1;
	}
	if(!Validate_Input(bbuilding)){
		document.getElementById("td_bbuilding").style.color='red';
		x=1;
	}
	if(!Validate_Input(building)){
		document.getElementById("td_building").style.color='red';
		x=1;
	}
	if(x==1){
		document.getElementById("td_err").innerHTML="<b>PLease Fill missing info in red</b>";
		return false;
	}
	return true;
}

//////////////////////delivery///////////////
function bookmark(){
	var title = 'Waseet Store';
	var url = 'http://www.waseetstore.com';
   	if (document.all)
     	window.external.AddFavorite(url, title);
   	else if (window.sidebar)
    	window.sidebar.addPanel(title, url, "")
   	else if (window.sidebar&&window.sidebar.addPanel)
    	 window.sidebar.addPanel(title,url,"");
}
function ChangeMenuClass(sel_menu,all_menu){
	for(var i=0;i<all_menu;i++){
		var ite='main_menu' + i;
		if(document.getElementById(ite)){
			document.getElementById(ite).className='menu2notselected';
		}
	}
	document.getElementById(sel_menu).className='menu2selected';
}

/////////////////////////
//company products
function $(id){
	return document.getElementById(id);
}

function finish(){
	var operation = $('operation');
	var common_form = $('common_form');
	
	operation.value = 1;
	common_form.submit();
}

function save(){
	var operation = $('operation');
	var common_form = $('common_form');
	
	operation.value = 0;
	common_form.submit();
}

function add_product(){
	
	var category_id = $('category_id').value;
	var subcategory_id = $('subcategory_id').value;
	var company_id = $('company_id').value;
	
	window.location = '?mode=a' + '&category_id=' + category_id + '&subcategory_id=' + subcategory_id + '&company_id=' + company_id;
	
}


//billing
function get_regions2(country_id){
	x_get_region_select_options(country_id,-1,'en',set_regions2);
}

function set_regions2(out){
	var opt = '<option value="">Select Region</option>' + out;
	select_options('region',opt);
}

function get_areas2(region_id){
	x_get_area_select_options(region_id,-1,'en',set_areas2)
}

function set_areas2(out){
	var opt = '<option value="">Select Area</option>' + out;
	select_options('area',opt);
}

///////////////
//billing

function get_regions3(country_id){
	x_get_region_select_options(country_id,$('region').value,'en',set_regions3);
}

function set_regions3(out){
	var opt = '<option value="">Select Region</option>' + out;
	select_options('bregion',opt);
}

function get_areas3(region_id){
	x_get_area_select_options(region_id,$('area').value,'en',set_areas3)
}

function set_areas3(out){
	var opt = '<option value="">Select Area</option>' + out;
	select_options('barea',opt);
}

