/*
 * Function to add code to set values of selected product ids separated by ','  
 * as into  hidden input field and code delete product id if user has unchecked the selected/checked product.
 */
function collectProdIds(chkElement)
{
	try 
    {
        if (chkElement.checked == true)
        {
            if ( document.getElementById('id') && document.getElementById('id').value) 
            {	
                if ( document.getElementById('id').value == "" ) 
                {
                    document.getElementById('id').value = chkElement.value;
                }
                else 
                {
                    document.getElementById('id').value = document.getElementById('id').value +"," + chkElement.value;
                }
            }
            else 
            {
                document.getElementById('id').value = chkElement.value;
            }
        }
        else
        {
            var compProdIds = document.getElementById('id').value.split(',');
            document.getElementById('id').value = '';
            for (i=0; i<compProdIds.length; i++)
            {
                if (compProdIds[i] == chkElement.value)
                {
                }
                else
                {
                    if(document.getElementById('id').value == "")
                    {
                        document.getElementById('id').value = compProdIds[i];                    
                    }
                    else
                    {
                        document.getElementById('id').value = document.getElementById('id').value +"," + compProdIds[i];
                    }
                }
            }
        }
	}
	catch(e) {
		document.getElementById('id').value = chkElement.value;
	}

	return (true);
}

function changeProductPage(page) {

    var q_string = "?";
    
    if ( document.frmSearch.length ) {
        e_count = document.frmSearch.length;
        for(i=0; i<e_count; i++){
            q_string += document.frmSearch[i].name +'='+ document.frmSearch[i].value +'&id='+document.getElementById('id').value+'&';
        }
    }
    if ( page != "" && page > 0 ) {
        q_string += "&x="+ page;
    }
    else {
        q_string += "&x=1";
    }
    window.location.href = q_string;
    return false;
}

function changeProductDisplay(page) {

    var q_string = "?";
   
    if ( page != "" && page > 0 ) {
        q_string += "&x="+ page;
    }
    else {
        q_string += "&x=1";
    }
    window.location.href = q_string;
   
    return false;
}
//Used in search.php
function changeProductList(page) {

    var q_string = "?";
   
    if ( page != "" && page > 0 ) {
        q_string += "&x="+ page;
    }
    else {
        q_string += "&x=1";
        page = 1;
    }
    //window.location.href = q_string;
    document.form1.x.value=page ;
    document.form1.submit() ;
    return false;
}
function changePage(page) {
    var q_string = "?";
	//alert(createQueryString(document.frmSearch));
    //alert(document.frmSearch.length+' '+document.getElementsByName('sString').value);
    if ( document.frmSearch.length ) {
        e_count = document.frmSearch.length;
        for(i=0; i<e_count; i++){
        //alert(i+' '+document.frmSearch[i].name);
            q_string += document.frmSearch[i].name +'='+ document.frmSearch[i].value+'&';
        }
    }

    if ( page != "" && page > 0 ) {
        q_string += "&x="+ page;
    }
    else {
        q_string += "&x=1";
    }
    window.location.href = q_string;
    return false;
}

function gotoURL(url) {
    document.location = url;
}


function reallyDel() {
		if(confirm("Do you really want to DELETE?")) {
				return true;
		}
		return false;
}

function selectOnlyOne( element ) {
	var el = document.getElementsByName(element.name) ;
	for( i=0; i<el.length; i++ ){
		if ( el[i].value != element.value ) {
			el[i].checked = false;
		}
	}	
}

function hideSelect() {
	var select_arr = window.document.getElementsByTagName("select");
	for (i=0; i<select_arr.length; i++) {
		select_arr[i].style.visibility = 'hidden';
	}
}
function showSelect() {
	var select_arr = window.document.getElementsByTagName("select");
	for (i=0; i<select_arr.length; i++) {
		select_arr[i].style.visibility = 'visible';
	}
}


/**
 * function onlyNumerics(e)
 * This function is used to restrict the input characters to 
 * digits [0-9] only, anyother key is neglected.
 *
 * @param	e		This is the Event object that has occurred. 
 *					This is mainly a hack for Mozilla browsers, in Internet Explorer it is not required.
 *
 * @return	boolean	true, if the key is accepted.
 *					false, if the key is not accepted.
 */
function onlyNumerics(e){
	var e = e? e : window.event;
	if(!e) 
		return;
	
	var key = 0;
	if (e.keycode) { 
		key = e.keycode; 
	} // for moz/fb, if keycode==0 use 'which'
	else if (typeof(e.which)!= 'undefined') { 
		key = e.which; 
	} 
	//alert(key);
	if ( (key>=48 && key<=57) || key == 8 || key ==0 ){
		return (true) ;
	}
	else{
		return (false);	
	}
}

/**
 * function toggleVisibilityOfElement( element )
 *
 * This function is used to change the visibility of 
 * element, where the "visible" and "hidden" and the 
 * css stylesheet entities having display property 
 * as "block" and "hidden" resp.
 */
function toggleVisibilityOfElement( element ) {
	if ( element.className == "visible" ) {	
		element.className = "hidden";
	}
	else {
		element.className = "visible";
	}
}

/**
 * This function is used to create the query string format of 
 * all the elements that are present within the scope of the 
 * form that has been passed as an argument.
 *
 * @param	form element	The form for which the query string is to be created.
 *
 * @return	querystring		the querystring format of all the elements is retunred back.
 */

function createQueryString(form) {
	var query_string = "";
	for (var i = 0; i < form.elements.length; i++) {

		switch (form.elements[i].type) {
			case ("text"): {
				if ( form.elements[i].value != "" ) {
					query_string += "&"+ form.elements[i].name +"="+ form.elements[i].value;
				}
				break;
			}
			case ("textarea"): {
				if ( form.elements[i].value != "" ) {
					query_string += "&"+ form.elements[i].name +"="+ form.elements[i].value;
				}
				break;
			}
			case ("hidden"): {
				if ( form.elements[i].value != "" ) {
					query_string += "&"+ form.elements[i].name +"="+ form.elements[i].value;
				}
				break;
			}
			case ("checkbox"): {
				if ( form.elements[i].checked ) {
					query_string += "&"+ form.elements[i].name +"="+ form.elements[i].value;
				}
				break;
			}
			case ("select-one"): {
				query_string += "&"+ form.elements[i].name 
									+"="+ form.elements[i].options[form.elements[i].selectedIndex].value;
				break;
			}
			case ("select-multiple"): {
				s_options = "";
				for ( j=0; j < form.elements[i].length; j++ ) {
					if ( form.elements[i].options[j].selected ) {
						s_options += ","+ form.elements[i].options[j].value;
					}
				}
				if (s_options != "") {
					s_options = s_options.substr(1);
					query_string += "&"+ form.elements[i].name +"="+ s_options;
				}
				break;
			}
			case ("radio"): {
				if ( form.elements[i].checked ) {
					query_string += "&"+ form.elements[i].name +"="+ form.elements[i].value;
				}
				break;
			}
/*			case ("submit"): {
				if ( form.elements[i].value != "" ) {
					query_string += "&"+ form.elements[i].name +"="+ form.elements[i].value;
				}
				break;
			}*/
		}
	}
	return (query_string);
}

function UploadAnotherImage(element)
{
    id = Math.random();
    main_div = document.createElement('div');
   // main_div.setAttribute('class', 'coloumn pt5');
  //  main_div.setAttribute('className', 'coloumn pt5');
    main_div.setAttribute('id', id);
    
    c1_div = document.createElement('div');
   // c1_div.setAttribute('class', 'coloumn pr10 w60');
   // c1_div.setAttribute('className', 'coloumn pr10 w60');
        
    prodImg_input = document.createElement('input');
    prodImg_input.setAttribute('type','file');
    prodImg_input.setAttribute('name','prod_img_url[]');
    prodImg_input.setAttribute('value','');
    prodImg_input.setAttribute('maxlength','250');
    prodImg_input.setAttribute('size','50');
    prodImg_input.setAttribute('class','inputbox');
    prodImg_input.setAttribute('className','inputbox');    
    
    prodImg_div = document.createElement('div');
    prodImg_div.setAttribute('class', 'coloumn pl10');
    prodImg_div.innerHTML = '<span class="txthelp">To remove, leave the fields empty.</span>';
    
    
    c1_div.appendChild(prodImg_input);
    c1_div.appendChild(prodImg_div);
    main_div.appendChild(c1_div);
    
    element.appendChild(main_div);
}

function removeElement(element, to_delete) {
    if ( element.removeChild(to_delete) ) {
    //if (1) {
        return (true);
    }
    else {
        return (false);
    }
}

function goThere(element)
{
    //var box = document.leftFrm.left_menus.options;
    var box = document.form1.left_menus.options;
    var where_to = box[box.selectedIndex].value;
    window.location = where_to;
}

function openWindowtoPostInqr(path,prod_id,prod_name,cat_id,product_type)
{
    window.open(path+"/product-post-inquiry.php?prod_id="+prod_id+"&prod_name="+prod_name+"&cat_id="+cat_id+"&product_type="+product_type+"","windowtodisply","width=600,height=500,top=200,left=150,toolbar=no,menubar=no,location=no,status=no,scrollbars=yes,resizable=yes");
    return false;
}


//Used in category add in admin bof
function toggleElement(obj) {
    
   if(obj.value == '0'){
        document.getElementById('typeid').className="showdiv";
          
    }else{
        document.getElementById('typeid').className="hidediv";
       
    }
}
//Used in category add in admin eof


function changePageWithExtra(page,string_e) {
    //var q_string = "?";
  var q_string = "";
    

    //if ( document.frmSearch.length ) {
      //  e_count = document.frmSearch.length;
      //  for(i=0; i<e_count; i++){
     //       q_string += document.frmSearch[i].name +'='+ document.frmSearch[i].value +'&';
     //   }
    //}
   
    
    if ( page != "" && page > 0 ) {
        q_string += "&x="+ page;
    }
    else {
        q_string += "&x=1";
    }
   
    // extra url variables pass    
     q_string+= string_e ;	
     sub_url= window.location.href ;	
	 pos =  sub_url.indexOf('?');
	
	if(navigator.appName.indexOf("Microsoft")!=-1){
		if(navigator.appVersion.indexOf("MSIE 6.0") !=-1){
			document.write();
		}
	}
	 //document.write();
	 if(pos >= 0){
		  pos = parseInt(pos);
          sub_url = sub_url.substr(0,pos);		
		  url = sub_url+"?"+q_string;		  
	 }else{
		  url = sub_url+"?"+q_string ;
	 }
	
	window.location = url;
    return true;   
}

var http_req_alerts;

function updateBrands(m_id) {
	
    var cboBrand = document.getElementById("brand_id");   
    cboBrand.options.length = 0;
  
    if(!m_id){
        cboBrand.options[0] = new Option("--SELECT--", "");	      
        return null;
    }
    
    http_req_alerts = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_req_alerts = new XMLHttpRequest();
    }else if (window.ActiveXObject) { // IE
        try {
            http_req_alerts = new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e) {
            try {
                http_req_alerts = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e) {}
        }
    }
    
    if (!http_req_alerts) {
        return false;
    } 
    var url = "getBrands.php?m_id="+m_id;    
  
    
    http_req_alerts.onreadystatechange = loadBrands;
    http_req_alerts.open('GET', url, true);
    http_req_alerts.send(null);
}

/**
* To Load account head depending on transaction type in payment in/out module
**/


function loadBrands(){
    var cboBrand = document.getElementById("brand_id");
   if(http_req_alerts.readyState==4){
   
        if (http_req_alerts.status == 200){
            var allThoseTypes = http_req_alerts.responseText;
            if(allThoseTypes){
                
                cboBrand.options[0] = new Option("--SELECT--", "");
                cboBrand.style.background ="#ffffff";
                var typeSplit =new Array();
                typeSplit = allThoseTypes.split("#");
            
              for (var i=0;i<typeSplit.length;i++){            
                var tmpStr =typeSplit[i];
                if(tmpStr){
                    var tmpArray = new Array();
                    tmpArray = tmpStr.split("|");
                    var tmpName =tmpArray[0];
                    var tmpVal = tmpArray[1];                   
                    if(tmpName!=''){
                        cboBrand.options[i+1] = new Option(tmpName, tmpVal);                                                
                    }
                }
              }           
            }
        }
    }else{        
          cboBrand.options[0] = new Option("Loading...", "");
          cboBrand.style.background = "#c3c3c3";
    }
}

function cleanState(id) {
	var state;	
	state = document.getElementById(id);	
	
	if (state.value == "First Name" || state.value == "Last Name" || state.value == "Company" || state.value == "Address" || state.value == "City" || state.value == "State" || state.value == "Phone/Mobile" || state.value == "Email" || state.value == "Comments" || state.value == "Verify")
    {	  	  	
        state.value = "";
        state.style.color = "#3a3737";
	    state.style.fontWeight = "bold";
    }
}

function changeState(id) {
	var state;	
	state = document.getElementById(id);	
	
    state.style.color = "#3a3737";
	state.style.fontWeight = "bold";
    
    if(id == 'first_name_id'){
        if(!(state.value))
        {
            state.value = "First Name";
            state.style.color = "#989494";
	        state.style.fontWeight = "normal";
        }
    }
    
    if(id == 'last_name_id'){
        if(!(state.value))
        {
            state.value = "Last Name";
            state.style.color = "#989494";
	        state.style.fontWeight = "normal";
        }
    }
    
    if(id == 'firm'){
        if(!(state.value))
        {
            state.value = "Company";
            state.style.color = "#989494";
	        state.style.fontWeight = "normal";
        }
    }
    
    if(id == 'address'){
        if(!(state.value))
        {
            state.value = "Address";
            state.style.color = "#989494";
	        state.style.fontWeight = "normal";
        }
    }
    
    if(id == 'city'){
        if(!(state.value))
        {
            state.value = "City";
            state.style.color = "#989494";
	        state.style.fontWeight = "normal";
        }
    }
    
    if(id == 'state'){
        if(!(state.value))
        {
            state.value = "State";
            state.style.color = "#989494";
	        state.style.fontWeight = "normal";
        }
    }
    
     if(id == 'contact_no'){
        if(!(state.value))
        {
            state.value = "Phone/Mobile";
            state.style.color = "#989494";
	        state.style.fontWeight = "normal";
        }
    }
    
    if(id == 'email'){
        if(!(state.value))
        {
            state.value = "Email";
            state.style.color = "#989494";
	        state.style.fontWeight = "normal";
        }
    }
    
    if(id == 'comments'){
        if(!(state.value))
        {
            state.value = "Comments";
            state.style.color = "#989494";
	        state.style.fontWeight = "normal";
        }
    }
    if(id == 'salutation'){
        if(!(state.value))
        {
            state.style.color = "#989494";
	        state.style.fontWeight = "normal";
        }
    }
    if(id == 'country'){
        if(!(state.value))
        {
            state.style.color = "#989494";
	        state.style.fontWeight = "normal";
        }
    }
    if(id == 'calculate'){
        if(!(state.value))
        {
            state.style.color = "#989494";
	        state.style.fontWeight = "normal";
        }
    }
}
