// $Id: form_scripts.js,v 1.1 2008/07/07 05:36:04 dungbt Exp $

var onload_handlers = new Array();
//
// Show/hide any tag by its id.
// parameters:
// id - element id
function fn_show_tag(id, status)
{
	if (document.getElementById(id)) {
		if (status == true || status == false) {
			document.getElementById(id).style.display = (status == true)?"none":"";
		} else {
			document.getElementById(id).style.display = (document.getElementById(id).style.display == "")?"none":"";
		}
	}
}

//



// Select text in text input
//
function fn_select_input(select)
{
	select.select();
}

//
// Disable/enable all form elements inside the tag 
// @id - element id
// @status - boolean (true to disable and false to enable)
function fn_disable_elements(id, status)
{

	node = document.getElementById(id);
	// Process INPUT types...
	inputs = node.getElementsByTagName('INPUT');
	for (i=0;i<inputs.length;i++) {
		node.getElementsByTagName('INPUT')[i].disabled = status;
	}

	// Process TEXTAREA type...
	texts = node.getElementsByTagName('TEXTAREA');
	for (i=0;i<texts.length;i++) {
		node.getElementsByTagName('TEXTAREA')[i].disabled = status;
	}

	// Process SELECT type...
	selects = node.getElementsByTagName('SELECT');
	for (i=0;i<selects.length;i++) {
		node.getElementsByTagName('SELECT')[i].disabled = status;
	}
}

//
// Open pop-up window with detailed image
//
function fn_open_popup_image(popup_script, image_width, image_height)
{
	if (image_width == 0) {
		image_width = 400;
	}
	if (image_height == 0) {
		image_height = 400;
	}
	image_width += 10;
	image_height += 10;

	if ((typeof(handle_popup_image)!='undefined') && (handle_popup_image.closed == false)) {
		handle_popup_image.close();
	}
    handle_popup_image = window.open(popup_script, 'popup_image', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,left=200,top=100,width=' + image_width + ',height=' + image_height + ',resizable=yes');
}

//
// Perform request via <SCRIPT> tag
//
function fn_http_request(script_id,url,status_msg,status_id)
{

	if (status_msg && status_id) {
		if (document.getElementById(status_id)) {
			document.getElementById(status_id).innerHTML = status_msg;
		}
	}

	var script_tag = document.getElementById(script_id);
	if (script_tag) {
		script_tag.parentNode.removeChild(script_tag);
	}

	script_tag = document.createElement('SCRIPT');
	script_tag.type = 'text/javascript';
	script_tag.language = 'javascript';
	script_tag.id = script_id;
    script_tag.src = url;
	document.body.appendChild(script_tag);
}

//
// String utility functions
//

// extract front part of string prior to searchString
function fn_str_get_front(mainStr,searchStr)
{
	foundOffset = mainStr.indexOf(searchStr)
	if (foundOffset == -1) {
		return null
	}
	
	return mainStr.substring(0,foundOffset)
}

// extract back end of string after searchString
function fn_str_get_end(mainStr,searchStr) 
{
	foundOffset = mainStr.indexOf(searchStr)
	if (foundOffset == -1) {
		return null
	}
	
	return mainStr.substring(foundOffset+searchStr.length,mainStr.length)
}

// insert insertString immediately before searchString
function fn_str_insert_string(mainStr,searchStr,insertStr) 
{
	var front = fn_str_get_front(mainStr,searchStr)
	var end = fn_str_get_end(mainStr,searchStr)

	if (front != null && end != null) {
		return front + insertStr + searchStr + end
	}
	
	return null
}
// remove deleteString
function fn_str_delete_string(mainStr,deleteStr) 
{
	return fn_str_replace_string(mainStr,deleteStr,"");
}
// replace searchString with replaceString
function fn_str_replace_string(mainStr,searchStr,replaceStr) 
{
	var front = fn_str_get_front(mainStr,searchStr)
	var end = fn_str_get_end(mainStr,searchStr)

	if (front != null && end != null) {
		return front + replaceStr + end
	}
	
	return null
}

// Compare 2 strings
// @haystack - where search
// @needle - what search
// @strict - exact compare or partial
function fn_compare_strings(haystack, needle, strict)
{
	if (strict == true) {
		return (haystack == needle);
	} else {
		return (haystack.indexOf(needle) == -1) ? false : true;
	}
}


//
// Check / uncheck all checkboxes in form
// @form_name - form name whose checkboxes should be checked or unchecked
// @checkbox_id - tag identifier of checkboxes that should be checked or unchecked
// @flag - can be true or false
function fn_check_all_checkboxes(form_name, flag, checkbox_id, strict)
{
	if (!checkbox_id) {
		checkbox_id = 'delete_checkbox';
	}
	if (typeof(strict) == 'undefined') {
		strict = true;
	}

	if(!(d_form = document.forms[form_name]))
		return false;		

	for(i=0; i < d_form.length; i++) {
		if (fn_compare_strings(d_form.elements[i].id, checkbox_id, strict) && !d_form.elements[i].disabled){			
			d_form.elements[i].checked = flag;
			}
	}
	return true;
}

// Check email address for validity
function fn_check_email(email, msg) {

	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;	
	if (filter.test(email)) {
		return true;
	} else{
		return false;
	}
}

//
// This function checks required fields and set a mark if something wrong
//
function fn_check_fields(form_name, extra_ids)
{
	var is_ok = true;
	var seqb_exists = document.getElementById('seqb');
	if (!document.forms[form_name]) {
		return false;
	}
	
	var is_ok = true;
	var set_mark = false;
	var mess="";
	var msg="";
	elms = document.forms[form_name].elements;
	for (i=0; i<elms.length; i++) {
		set_mark = false;		
			var str=elms[i].id;			
			var lable=str.substr(2,str.length);
			str=str.substr(0,1);	

		if (required_fields[elms[i].id] == 'Y' && fn_is_blank(elms[i].value) == true) {
			if (str=="b"){
				mess=mess+ message+  billing_address + ">>"+ lang_profile[elms[i].id] + "\n";	
			}
			else if(document.getElementById('seqb').checked == false && elms[i].id.substr(0,2)=='s_') {							
				mess=mess+ message+ shipping_address + ">>"+ lang_profile[elms[i].id] + "\n";	
			}
			else if(document.getElementById('seqb').checked == true && elms[i].id.substr(0,2)=='s_') {							
				mess=mess+ "";	
			}
				
			else {
				mess=mess + message + lang_profile[elms[i].id] + "\n";
			}		
			//mess=mess + message + lang_profile[elms[i].id] + "\n";
			
			is_ok = false;				
			set_mark = true;
			/*if (seqb_exists && document.getElementById('seqb').checked == true && elms[i].id.substr(0,2)=='s_') {
				document.getElementById('seqb').click();
			}*/
		}else{
		
		// Check the email field		
		if (extra_ids[elms[i].id] == 'E' && elms[i].value !="") {
			if (fn_check_email(elms[i].value, email_invalid) == false) {
				if (required_fields[elms[i].id] == 'Y' || fn_is_blank(elms[i].value) == false) {
					is_ok = false;
					mess=mess + email_invalid+"\n";
					set_mark = true;
				}
			}
		// Check for integer field
		} else if (extra_ids[elms[i].id] == 'I' && (elms[i].value)!="") {
			if (fn_check_phone(elms[i].value) == false) {
				if (required_fields[elms[i].id] == 'Y' || fn_is_blank(elms[i].value) == false) {
					mess=mess + "The "+elms[i].id + number_invalid +"\n";
					is_ok = false;
					set_mark = true;
				}
			}
			
		// Check for user field
		} else if (extra_ids[elms[i].id] == 'U') {			
			if (fn_check_user(elms[i].value) == false && (elms[i].value)!="") {
				if (required_fields[elms[i].id] == 'Y' || fn_is_blank(elms[i].value) == false) {
					mess=mess+ user + not_contain_special +"\n";					
					is_ok = false;
					set_mark = true;
				}				
			}
			
		// Check for pass field
		} else if (extra_ids[elms[i].id] == 'P' && elms[i].value!="" && elms[i].readonly==false) {			
			var str="";
			str=elms[i].id;
			var lable=str.substr(2,str.length);
			str=str.substr(0,1);				
			msg=elms[i].value;
			if(msg.length <6) {
				if(str=="p" && elms[i].id.substr(elms[i].id.length-1,1)=="2"){
					mess=mess+ confirm_password + longer_than+ character +"\n ";
				}
				
				else if(str=="p" && elms[i].id.substr(elms[i].id.length-1,1)=="1"){
					mess=mess+ password + longer_than+ character +"\n ";
				}								
					is_ok = false;
					set_mark = true;
			}
			if (fn_check_pass(elms[i].value) == false && msg.length >=6) {
				
				if (required_fields[elms[i].id] == 'Y' || fn_is_blank(elms[i].value) == false) {
					if(str=="p" && elms[i].id.substr(elms[i].id.length-1,1)=="2"){
						mess=mess+ confirm_password + not_contain_special +"\n";
					}
					
					else if(str=="p" && elms[i].id.substr(elms[i].id.length-1,1)=="1"){
						mess=mess+ password + not_contain_special +"\n";
					}						
					is_ok = false;
					set_mark = true;
				}
				
				
			}
						
		// Check for blank value
		} 
		}
		if (document.getElementById('status_'+elms[i].id)) {
			document.getElementById('status_'+elms[i].id).innerHTML = (set_mark == true)? warning_mark : "&nbsp;";
		}
		
	}	
	if (is_ok == false) {
		alert(mess);
	}
	return is_ok;
}

//
// Checks if the value is blank
//
function fn_is_blank(val){
	if (val==null){
		return true;
	}
	for (var i=0; i<val.length; i++) {
		if((val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){
			return false;
		}
	}
	return true;
}

//
// Checks if the value is integer
//
function fn_is_integer(val)
{
	if(fn_is_blank(val)){
		return false;
	}
	for(var i=0; i<val.length; i++){
		if (!fn_is_digit(val.charAt(i))) {
			return false;
		}
	}
	return true;
}

//
// Checks if the value is digit
//
function fn_is_digit(num)
{
	if(num.length>1){
		return false;
	}
	var string="1234567890().- ,_";
	if (string.indexOf(num)!=-1){
		return true;
	}
	return false;
}

//
// Execute functions on page load
//
function fn_load_handlers()
{
	var i=0;
	for (i=0; i<onload_handlers.length; i++) {
		eval(onload_handlers[i]);
	}
}

//
// Checks if the value user
//
function fn_check_user(user) {

//	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;	
	var filter=/^([\w-]+(?:\.[\w-]+)*)$/;		
	//alert(filter);
	if (filter.test(user)) {
		return true;
	} else{
		return false;
	}
}
// Checks pass
function fn_check_pass(pass) {
	var filter=/^([a-z,0-9]{6,40}(?:\.[a-z,0-9]{6})?)$/;	
	if (filter.test(pass)) {	
		return true;
	} else{		
		return false;
	}
}

// Checks first & last Name

function fn_check_name(name) {
	var value=trim(name);
	if(value.length==0){
		return false;
	}
	else {
		return true;
	}
}

function fn_check_phone(value) {
//	var filter=/^([\w-]+(?:\.[\w-]+)*)$/;	
	var filter=/^[0-9()-_' '+.]+$/;
	if (filter.test(value)) {
		return true;
	} else{
		return false;
	}
}

function fn_change_pass(){
	if(document.getElementById('pass_check').checked==true){
		document.getElementById('pass').value=1;
		document.getElementById('password1').readOnly=false;
		document.getElementById('password2').readOnly=false;
		document.getElementById('password1').value="";
		document.getElementById('password2').value="";
	}
	else{
		document.getElementById('pass').value=0;
		document.getElementById('password1').readOnly=true;
		document.getElementById('password2').readOnly=true;
		document.getElementById('password1').value="abcABC1234";
		document.getElementById('password2').value="abcABC1234";
	}	
}




