/** common & specific form function **/

/**
 * Submits a FORM by ID
 */
function SubmitForm( formId ){
	
	if( $$(formId) ){
		
		$$(formId).submit();
	}

}



function submitFormOnce( formId ){
	
	if( $$(formId) ){
		
		$$(formId).submit();
	}

}


/**
 * Validate field value
 * 
 * @param field
 * @param type
 * @return
 */
function ValidateField( field, type ){

	
	data = 'action=fieldsvalidate';
	
	val = document.getElementById(field).value;
	
	data += "&val=" + val + "&name=" + field + "&type=" + type;
			
	SendData("ajax.php",data);
}

/**
 * Collects FORM data
 * 
 * @param formId
 * @return
 */
function getFormData( formId ){
	
	var data = '';
	var form = document.getElementById( formId );
	
	for( i=0; i<form.elements.length; i++ ){
					
		switch( form.elements[i].type ){
			
			case 'checkbox':{
				
				if( form.elements[i].checked==true ){
					data += form.elements[i].name + "=" + 't' + "&";
				}
				else data += form.elements[i].name + "=" + 'f' + "&";
				
			}break;
			
			case 'radio':{
				
				if( form.elements[i].checked==true )
							data += form.elements[i].name + "=" + form.elements[i].value + "&";					
				
			}break;
			
			default:{
				data += form.elements[i].name + "=" + form.elements[i].value + "&";	
			}	
		}
	}

	return data;
}


/**
 * Alert Validation Errot message
 * @param filed
 * @param status
 * @return
 */
function ShowValidateErrorMessage( filed, status ){
	
	//UnblockAll();
	
	alert( status + " for field '" + filed + "'" );
	if( document.getElementById( filed ) ){
	
		document.getElementById( filed ).focus();
	}
}


/**
 * Show sidebox erroe message
 * @param field
 * @param errorCode
 * @param color
 * @return
 */
function ShowFieldErrorMess( field, errorCode, color ){
	
	//UnblockAll();
	//alert(errorCode);
	
	color = color ? color : '#FF0000';
	
	posX = findPosX( $$(field) );		
	posY = findPosY( $$(field) );	

	switch( errorCode ){ 
		
		case '-1': {
				document.getElementById('error_div').innerHTML = "";
				document.getElementById('error_div').style.display = "none";
		}break;

		default:{
				$$('error_div').innerHTML = errorCode;
				$$('error_div').style.color = color;
				$$('error_div').style.left = posX + 260 + 'px';
				$$('error_div').style.top = posY + 'px';
				$$('error_div').style.display = "inline";					
		}
	}
			
}


/**
 * Style input
 * 
 * @param obj
 * @return
 */
function FocusField( obj ){
	
	if( obj ){
		
		obj.style.border = '1px solid #000';
	}
}


/**
 * Style input
 * 
 * @param obj
 * @return
 */
function BlurField( obj ){
	
	if( obj ){
		
		obj.style.border = '1px solid #89AF3E';
	}	
}


/**
 * change site currency
 *  
 * @param obj := html select obj
 * @return
 */
function ChangeCurrency( obj ){
	
	if( obj && obj.value ){
		
		var currency = obj.value;
		
		data = 'action=change_currency&currency=' + currency;
				
		SendData("ajax.php",data);
	}
}


/**
 * Change delivery zone
 * @param obj
 * @return
 */
function ChangeZone( obj ){
	
	if( obj && obj.value ){
		
		data = 'action=change_zone&zone=' + obj.value;			
		SendData("ajax.php",data);
	}
}



//TODO make it better
function gotoSearchURL( action ){
	
	searchString = '';
	
	if( $$('search') ) {
		searchString = $$('search').value;
	}
	
	if( action && searchString ){
		
		url = 'index.php?action=' + action + '&search=' + searchString;
    	document.location = url;
    }
}


/**
 * Gets maxlenght for TEXTAREA
 * @param Object
 * @param MaxLen
 * @return
 */
function imposeMaxLength( Object, MaxLen )
{
	if( Object && Object.value.length ) {
		
		return (Object.value.length <= MaxLen);
	}
}


/**
 * Clear defautl value of INPUT
 * 
 * @param obj
 * @return
 */
function doClear( obj ) {
	
    if ( obj && obj.value == obj.defaultValue ) {
    	
    	obj.value = "";
    }
}

/**
 * Set back default value to INPUT if value is empty
 * 
 * @param obj
 * @return
 */
function doReset( obj ) {
	
    if ( obj && obj.value == "" ) {
    	
    	obj.value = obj.defaultValue;
    }
}





/**
 * Show other products categories menu
 * @param button := a tag element
 * @return
 */
function showOtherCategories( button ) 
{

	var obj = $$('categories-other');
	
	if( obj )
	{
		
		if( obj.style.display == "none" )
		{
			
			obj.style.display = "block";
			button.className = 'active';
		}
		else {
			obj.style.display = "none";
			button.className = 'inactive';
		}
	}
}




function RateProduct( pid ){
	
	if( $$('vote') ){
		
		val = $$('vote').value;
		
		data = 'action=rate_product&id=' + pid + '&val=' + val;
		SendData( 'ajax.php', data );
	}
		
}






