$(function(){
	$('a[rel="_blank"]').bind('click', function(e){
		e.preventDefault();
		var _link = $(this).attr('href');
		window.open(_link, '_blank');
	});
	
	$('#home-btn-profile').bind('click', function(){
		window.location.href="company.php";
	});
	
	$('#home-btn-product').bind('click', function(){
		window.location.href="products.php";
	})
});

function trim(str) { 
	return str.replace(/(^\s*)|(\s*$)/g,"");
}

function checkAll(fieldName) {
	var box = document.getElementsByName(fieldName);
	for(i=0;i < box.length; i++) {
		box[i].checked = true;
	}
}

function unCheckAll(fieldName) {
	var box = document.getElementsByName(fieldName);
	for(i=0;i < box.length; i++) {
		box[i].checked = false;
	}
}

function emailCheck (emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
		
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) 
		return false

	var user=matchArray[1]
	var domain=matchArray[2]
	
	if (user.match(userPat)==null) 
		return false

	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		  for (var i=1;i<=4;i++) {
			if (IPArray[i]>255)
				return false
		}
		return true
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null)
		return false

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3)
	   return false

	if (len<2)
	   return false

	return true;
}

function genDate(yearID,monthID,dayID) {
	var dateset1 = new Array(31,29,31,30,31,30,31,31,30,31,30,31)
	var dateset2 = new Array(31,28,31,30,31,30,31,31,30,31,30,31)
	var remonth = document.getElementById(monthID).selectedIndex

	if ((document.getElementById(yearID).value % 4) == 0)
		document.getElementById(dayID).options.length = dateset1[remonth];
	else
		document.getElementById(dayID).options.length = dateset2[remonth];
	
	for(i=1;i<=document.getElementById(dayID).options.length;i++){
		document.getElementById(dayID).options[i-1].text = i;
		document.getElementById(dayID).options[i-1].value = i;
	}
}

function numOnly(str) {
	var output = "";
	for (i=0; i<str.value.length; i++) {
		if (str.value.substr(i,1) >= "0" && str.value.substr(i,1) <= "9") {
			output += str.value.substr(i,1);
		}
	}
	str.value = output;
}

function handleEnter (functionName, event) {	
	if (event.keyCode == 13) {
		eval(functionName);
		return false;
	} else {
		return true;
	}
}

function stopRKey(evt) {
	var evt = (evt) ? evt : ((event) ? event : null);
	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	if ((evt.keyCode == 13) && ((node.type=="text") || (node.type=="password"))) {
	   	return false;
	}
}

function checkCheckBox(checkBoxName) {
	var boxcheck = false;
	var itemID = document.getElementsByName(checkBoxName);

	for(i=0; i<itemID.length; i++){
		if(itemID[i].checked){
			boxcheck = true;
			break;
		}
	}
	return boxcheck;
}

function openNoMenuWindow(page,pageName,width,height) {
	window.open(page, pageName, 'toolbar=no,location=no,directories=no,menubar=no,status=no,scrollbars=yes,resizable=yes,width='+width+',height='+height);	
} 

function rawurlencode (str) {
    var hexStr = function (dec) {
        return '%' + dec.toString(16).toUpperCase();
    };

    var ret = '',unreserved = /[\w.~-]/;
    str = (str+'').toString();

    for (var i = 0, dl = str.length; i < dl; i++) {
        var ch = str.charAt(i);
        if (unreserved.test(ch)) {
            ret += ch;
        } else {
            var code = str.charCodeAt(i);
            
            if (code < 128) { 
                ret += hexStr(code);
            } else if (code >= 128 && code < 2048) { 
                ret += hexStr((code >> 6) | 0xC0);
                ret += hexStr((code & 0x3F) | 0x80);
            } else if (code >= 2048 && code < 65536) { 
                ret += hexStr((code >> 12) | 0xE0);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            } else if (code >= 65536) { 
                ret += hexStr((code >> 18) | 0xF0);
                ret += hexStr(((code >> 12) & 0x3F) | 0x80);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
        }
    }
    return ret;
}

function printPartOfInfo(partData) {
	
	 var iframe =document.createElement('iframe');
	 iframe.width=0;
	 iframe.height=0;
	 document.body.appendChild(iframe);

	 if(iframe) {   
	
		var ifWin = iframe.contentWindow || iframe;
		ifWin.document.open();
		ifWin.document.write(partData);
		ifWin.document.close();
		ifWin.focus();
		ifWin.print();
	 }
	iframe.parentNode.removeChild(iframe);
}

function replaceString(str,currentWord,replaceWord) {
	
	var intIndexOfMatch = str.indexOf(currentWord);

	while (intIndexOfMatch != -1){
	
		str = str.replace(currentWord,replaceWord)
		intIndexOfMatch = str.indexOf(currentWord);
	}
	return str;
}

function resize(left_id, right_id) {    
	var left_col=($("#"+left_id).height());    
	var right_col=($("#"+right_id).height());    
	
	if(left_col > right_col) {        
		$("#"+right_id).height($("#"+left_id).height());
	} else if(right_col > left_col) {       
		$("#"+left_id).height($("#"+right_id).height());
	} 
}

function checkForm(data){
	
/*
	// Sample of using the checking 
	$('#reg_member').submit( function (){	
		var data = {
			emptyField : ['username', 'password', 'password2', 'name', 'email'],
			sameField : [['password', 'password2']],
			selectField : ['news_letter_choice', 'buy_heath'] // put class in here
			atLeast : [{ number: 2, elements: [] }]
		}
		return (anglia.checkForm(data) && anglia.member.username);
	});
 */
	
	var emptyChecking = data.emptyField || null;
	var sameFieldChecking = data.sameField || null;
	var selectFieldChecking = data.selectField || null;
	var atLeastChecking = data.atLeast || null;
	var warningFieldColor = '#fff0f0';
	var correctFieldColor = '#ffffff';
	
	var forCheckMail = true;
	var formBoo = true;
	
	var re_left_space = /^\s+/;
	var re_email = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
	var re_no_space = /^[a-z]([0-9a-z_])+$/i;
	
	$('.compulsory').prev().css({'background-color':correctFieldColor});
	
	// Empty field validation
	if(emptyChecking != null){
		for(var i = 0; i < emptyChecking.length; i++){
			
			var thisboo = true;
			var el = $('#' + emptyChecking[i]);
			var g = el.val();
			
			if(g.length == 0){
				thisboo = false;
			} else 	if(el.hasClass('chk_mail')){
				if(!re_email.test(g)) thisboo = false;
			} else 	if(el.hasClass('no_space')){
				if(!re_no_space.test(g)) thisboo = false;
			}
			
			if(thisboo == false){
				el.css('background-color', warningFieldColor);
				formBoo = false;
			}
		}
	}
	// password form checking
	if(sameFieldChecking != null){
		
		for(var i = 0; i < sameFieldChecking.length; i++){
			var cur_chk_array = sameFieldChecking[i];
			var a = trim($('#'+cur_chk_array[0]).val());
			var b = trim($('#'+cur_chk_array[1]).val());
			if( a != '' ||  b != ''){
				if(	a != b ){
					$('#' + cur_chk_array[1]).css('background-color', warningFieldColor);
					formBoo = false;
				}
			}
		}
	}

	if(selectFieldChecking != null){
		for(var i = 0; i < selectFieldChecking.length; i ++ ){
			var elements = $('.'+selectFieldChecking[i]);
			if(!elements.is(':checked')) {
				$('.'+selectFieldChecking[i]+':first').before(errorDiv);
			}
		}
	}
	
	if(atLeastChecking != null){
		
		var atLeastPartBool = true;
	
		for(var i = 0; i < atLeastChecking.length; i++){
			var chkObj = atLeastChecking[i];
			var chkElements = chkObj.elements;
			var number = chkObj.number;
			
			if(chkElements.length == 0) continue;
			
			var isOk_num = 0;
			for(var g = 0 ; g < chkElements.length ; g++){
				var _boo = true;
				var _el = $('#' + chkElements[g]);
				
				if(_el.val() == ''){
					_boo = false;
				}

				if(_boo){
					isOk_num++;
				}
			}
			
			if(isOk_num < number){
				atLeastPartBool = false;
				for(var f = 0 ; f < chkElements.length ; f++){
					var _boo = true;
					var _el = $('#' + chkElements[f]);
					if(_el.val() != ''){
						_boo = false;
					}
					
					if(_boo){
						_el.css('background-color', warningFieldColor);
						break;
					}
				}
			} 
		}
	}
	
	$('.chk_mail').each(function(){
		if($(this).val() != ''){
			if(!re_email.test($(this).val())) {
				forCheckMail = false;
				if($(this).css('background-color') != warningFieldColor)
					$(this).css('background-color', warningFieldColor);							
			}
		}
	});
	
	formBoo = formBoo && atLeastPartBool && forCheckMail;
	
	return formBoo;	
}
