$j = jQuery.noConflict();
// JavaScript Document



function getXMLObject()
{
    var requestObj;
    if(window.XMLHttpRequest) {
        requestObj = new XMLHttpRequest();
    }
    else if(window.ActiveXObject) {
        requestObj = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return requestObj;
}

function voteLink(str, link_id)
{
    var requestObj = getXMLObject();

    if(requestObj)
    {
        var url="rate_link.php?str="+str +"&link_id="+link_id;

        requestObj.open("GET",url,true);
        requestObj.onreadystatechange = function()
        {
            if(requestObj.readyState==4 && requestObj.status == 200)
            {
                var str_image = requestObj.responseText;
                document.getElementById('lnk'+link_id).innerHTML=str_image;
                document.getElementById('rate_Lnk'+link_id).innerHTML='';
                document.getElementById('rate_Lnk1'+link_id).innerHTML='';
            }
        }
        requestObj.send(null);
    }
}
/*
function addPhoneLinks()
{
	if(document.getElementById('addlink').style.display=='none') {

		document.getElementById('addlink').style.display='block';
	} else {

		document.getElementById('addlink').style.display='none';
	}
}

function changeShip()
{
	document.cart_quantity.shippingID.value=document.cart_quantity.shipping[document.cart_quantity.shipping.selectedIndex].value;

}

function linkValidate()
{
	var strMsg = "Sorry! We can't complete your request. The following information is missing:\n\n";
	var msg="";
	if(document.getElementById('link_title').value=="")  {
		msg +="Link title should not be empty.\n";
	}
	if(document.getElementById('link').value=="")  {
		msg +="Link field should not be empty.\n";
	}
	/*if(document.getElementById('link_description').value=="")  {
		msg +="Link description should not be empty.";
	}
	if(msg!="")
	{
		alert(strMsg+msg);
		return false;
	}
	return true;
}



function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
}
 */
function checkVideo(obj)
{

    var MsgStr = "Sorry, we cannot complete your request.\nPlease provide us the missing or incorrect information below:\n\n";
    var str="";

    if(obj.video_title.value=="") {
        str +="Video title shouldn't be blank.\n";
    }
    if(obj.video_url.value=="") {
        str +="Video URL shouldn't be blank.\n";
    }
    if(str!='') {

        alert(MsgStr + str);
        return false;
    } else {
        return true;
    }

}

function rate_video(rate_point, video_id)
{
    var requestObj = getXMLObject();

    if(requestObj)
    {
        var url="rate_video.php?rate_point="+rate_point +"&video_id="+video_id;

        requestObj.open("GET",url,true);
        requestObj.onreadystatechange = function()
        {
            if(requestObj.readyState==4 && requestObj.status == 200)
            {
                var str_image = requestObj.responseText;
                var imagename= str_image.split('~', 3);
                document.getElementById('current_rating').innerHTML = imagename[0];
                document.getElementById('rating_no').innerHTML ="("+ imagename[2] + " out of 5)";

                if(imagename[1]==0) {
                    document.getElementById('voted').innerHTML ="You have already rated this phone video.";
                }
            }
        }
        requestObj.send(null);
    }
}


function checkForm() {
    var error = 0;
    var error_message = "Errors have occured during the process of your form.\n\nPlease make the following corrections:\n\n";

    var review = document.product_reviews_write.review.value;
    /* if(document.product_reviews_write.review_title.value=="")
  {
  	 error_message = error_message + "* Review title shouldn't be empty.";
     error = 1;
  }*/
    if (review.length < 50) {
        error_message = error_message + "\n* The \'Review Text\' must have at least 50 characters.";
        error = 1;
    }

    /*if ((document.product_reviews_write.rating[0].checked) || (document.product_reviews_write.rating[1].checked) || (document.product_reviews_write.rating[2].checked) || (document.product_reviews_write.rating[3].checked) || (document.product_reviews_write.rating[4].checked)) {
  } else {
    error_message = error_message + "\n* You must rate the product for your review.";
    error = 1;
  }*/

    if (error == 1) {
        alert(error_message);
        return false;
    } else {
        return true;
    }
}



function selectionChange(delta,aid, grp,products_id)
{
    var requestObj = getXMLObject();
    if(requestObj)
    {
        var url="accessories_helper.php?query="+delta+","+aid+","+grp+","+products_id;

        requestObj.open("GET",url,true);
        requestObj.onreadystatechange = function()
        {
            if(requestObj.readyState==4 && requestObj.status == 200)
            {
                var sum = requestObj.responseText;
                // var strAccess =sum.split('~',2);
                document.getElementById('summary').innerHTML = sum;
            // document.getElementById('totalprice').innerHTML =strAccess[1];
            }
        }
        requestObj.send(null);
    }
}

function phone_finder_update(url, string)
{

    var xmlHttp = getXMLObject();

    document.getElementById('product_display').innerHTML='<div id="featuredProd"><div style="width:670px;"><div style="overflow:hidden;"><div style="width:6px; float:left;"></div></div><div align="center" style="overflow:hidden; "><div style="width:700px;"><div style="width:100%;height:225px; font-size:16px; color:#000000; margin-top:100px;"><img src="images/ajax_loader/ajax-loader.gif" alt="GSM Liberty" style="vertical-align:middle;" title="GSM Liberty" border="0" />&nbsp;Loading.........</div><div style="clear:both; height:9px;"></div></div><div style="clear:both"></div></div></div><div style="overflow:hidden;"><div style="width:11px; float:left;"></div><div style=" float:left; width:700px; height:13px;"></div><div style="width:11px; float:right;"></div></div><div style="clear:both; height:15px;"></div></div>';

    //alert('test');
    if(xmlHttp)
    {
        var manf = document.getElementById('manufacturer_id').value;
        url=url+"?counter="+string+"&mn="+manf;
        url=url+"&sid="+Math.random();
        //alert(url);
        xmlHttp.onreadystatechange=function()
        {
            if((xmlHttp.readyState==4) && (xmlHttp.status == 200))
            {
                phoneUupdateChanged(xmlHttp.responseText);

            }
        }
    }
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function phoneUupdateChanged(result)
{

    var strAccess = result.split('<>',9);

    var features_id = 'feature'+strAccess[3];
    var fea_idd = 'fea'+strAccess[3];
    var range_id = 'range'+strAccess[6];
    var r_id = 'r'+strAccess[6];
    var idsarr = strAccess[7].split(':');
    var idslen = idsarr.length;
    var imgname = "images/box.jpg";
    var bgcol = "#F2F2F4";

    document.getElementById('product_display').innerHTML = strAccess[0];
    //document.getElementById('featuresName').innerHTML = strAccess[1];
    //document.getElementById('noOfFeaturesPorducts').innerHTML = strAccess[2];

    if(strAccess[6])
    {
        document.getElementById(range_id).innerHTML = '<img src="'+strAccess[4]+'"  border="none" />';
        //document.getElementById(r_id).style.background = strAccess[5];

        for(var i=0; i<(idslen-1); i++)
        {
            var rowid = idsarr[i];


            document.getElementById('range'+rowid).innerHTML = '<img src="'+imgname+'"  border="none" />';
        //document.getElementById('r'+rowid).style.background = bgcol;
        }
    }
    else
    {

        document.getElementById(features_id).innerHTML = '<img src="'+strAccess[4]+'"  border="none" />';
    //document.getElementById(fea_idd).style.background = strAccess[5];
    }

    document.getElementById('dispTab').innerHTML = strAccess[8];

}

/*
function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}

 */

function unlock_mobile_tabs(url,str,divID)
{

    var requestObj = getXMLObject();

    document.getElementById('product_display').innerHTML='<div style="width:670px;;"><div style="overflow:hidden;"><div style="width:6px; float:left;"></div><div ></div><div style="width:6px; float:right;"></div></div><div align="center" style="overflow:hidden; "><div style="width:700px;"><div id="row" align="center" style="width:100%;height:225px; font-size:16px; color:#000000; margin-top:100px;" class="product-bottom123"><img src="images/ajax_loader/ajax-loader.gif" alt="GSM Liberty" style="vertical-align:middle;" title="GSM Liberty" border="0" />&nbsp;<span class="manuf">Loading.........</span></div></div></div><div style="clear:both;"></div></div>';



    if(requestObj)
    {
        mnid = document.getElementById('manufacturer_id').value;

        url=url+"?q="+str+"&m="+mnid;
        url=url+"&sid="+Math.random();

        requestObj.onreadystatechange=function()
        {
            if((requestObj.readyState==4) && (requestObj.status == 200))
            {

                chengedTab(requestObj.responseText);
            }
        }
    }

    requestObj.open("GET",url,true);
    requestObj.send(null);
}

function chengedTab(result)
{
    var strAccess = result.split('~', 2);
    document.getElementById('dispTab').innerHTML = strAccess[0];
    if (strAccess[1] != undefined)
        document.getElementById('product_display').innerHTML = strAccess[1];
    else
        document.getElementById('product_display').innerHTML = '<div>No products in this category.</div>';
    $j('#anchorDrop').trigger('change');

}


function calculate_checkout_shipping(url,str, thisvalue,divID)
{

    var xmlHttp = getXMLObject();

    //document.getElementById('product_display').innerHTML='<div style="width:723px;;"><div style="overflow:hidden;"><div style="width:6px; float:left;"><img src="images/left-edge.png" width="6" height="17" align="left" /></div><div style="background-color:#ffffff; float:left; width:711px; height:17px;"></div><div style="width:6px; float:right;"><img src="images/right-edge.png" width="6" height="17" align="right" /></div></div><div align="center" style="overflow:hidden; background:url(images/icons-grnd.gif) repeat-x left top #C6C8CA;"><div style="width:700px;"><div id="row" align="center" style="width:100%;height:225px; font-size:16px; color:#000000; margin-top:100px;" class="product-bottom123"><img src="images/ajax_loader/ajax-loader.gif" alt="GSM Liberty" style="vertical-align:middle;" title="GSM Liberty" border="0" />&nbsp;Loading.........</div></div></div><div style="clear:both;"></div></div>';

    if(xmlHttp)
    {
        url = url+"?q="+str+'&countryCode='+thisvalue;
        url = url+"&sid="+Math.random();
        //alert(url);
        xmlHttp.onreadystatechange=function()
        {
            if((xmlHttp.readyState==4) && (xmlHttp.status == 200))
            {  //alert(xmlHttp.responseText);
                var strShip = xmlHttp.responseText
                var strShipCal = strShip.split('~',2);
                $j('#shiping_prices').html(strShipCal[0])
                //document.getElementById('shipDiv').innerHTML = strShipCal[0];
                $j('#cartTotal').html('---')
                $j('#shipPrice').html()
                $j('#shiping_prices').trigger('change');
            }
        }
    }
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function shippingPriceDisplay(url,str, divID)
{

    var xmlHttp = getXMLObject();
    //alert(str);
    //alert(document.getElementById('shipping').value);
    if(xmlHttp)
    {
        url = url+"&qString="+str;
        url = url+"&sid="+Math.random();
        //alert(url);
        xmlHttp.onreadystatechange=function()
        {
            if((xmlHttp.readyState==4) && (xmlHttp.status == 200))
            {
                alert(xmlHttp.responseText);
                //shiping_prices(xmlHttp.responseText, divID);
                document.getElementById(shipPrice).innerHTML = result;


            }
        }
    }
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}


function get_accessories(strURL, str_id, divID)
{
    var requestObj = getXMLObject();
    if(requestObj)
    {
        var url = strURL+"?str_id="+str_id;
        url = url+"&sid="+Math.random();

        requestObj.open("GET",url,true);
        requestObj.onreadystatechange = function()
        {
            if(requestObj.readyState==4 && requestObj.status == 200)
            {
                var strResponse = requestObj.responseText;
                var divDispId =  divID+str_id;

                document.getElementById(divDispId).innerHTML = strResponse;
            }
        }
        requestObj.send(null);
    }
}

function getGSMModel()
{
    var manufacturers_id = document.getElementById('manu_id').value;

    var requestObj;
    requestObj = getXMLObject();

    if(requestObj)
    {
        var url="manufacturer_product.php?manufacturers_id="+manufacturers_id;
        var funlock = document.getElementById('f_unlock_2');
        requestObj.open("GET",url,true);
        requestObj.onreadystatechange = function()
        {
            if(requestObj.readyState==4 && requestObj.status==200)
            {
                document.getElementById('loader').style.display = 'none';
                document.getElementById('lbl1').innerHTML='Select your model';
                var obj = eval("(" + requestObj.responseText + ")");
                document.getElementById('modname1').innerHTML=obj.select;
                document.getElementById('manufacturer-general').style.display = 'none'
                document.getElementById('manufacturer-models').innerHTML=obj.list;
            } else {
                document.getElementById('loader').style.display = 'block';;
            }
        }

        requestObj.send(null);
    }
}


function AllMobilePhones(url, path)
{
    var requestObj1;
    requestObj1 = getXMLObject();
    if(requestObj1)
    {
        var url = url;
    }
    requestObj1.open("GET",url,true);

    requestObj1.onreadystatechange = function()
    {
        if(requestObj1.readyState==4 && requestObj1.status==200)
        {

            window.location.href = path;
        }
    }
    requestObj1.send(null);
}

function popupWindow(url) {
    window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=450,height=150,screenX=150,screenY=150,top=150,left=150')
}


function removeDiv()
{
    document.getElementById('fly_main').style.display = "none";
}

/*
function setTab($jval)
{
	document.getElementById('tab').innerHTML = "";
	document.getElementById('tab').innerHTML = '<span class="b_unlock">Unlock your phone</span><a href="#" class="b_buy">Buy unlocked phones</a>';
}
 */
function searchRemove(x) {
    if ((x.value == 'find it here') || (x.value == 'find it here�')) {
        x.value = '';
    }
}

function searchField(x) {
    if (x.value == '') {
        x.value = 'find it here...';
    }
}


$j(document).ready(function(){
    $j('#tabs>div').hide();
    $j('#tabs>div:first').show();
    $j('#tabs>ul li:first').addClass('active');
    $j('#tabs>ul li a').click(function(){
        $j('#tabs>ul li').removeClass('active');
        $j(this).parent().addClass('active'); 
        var currentTab = $j(this).attr('href'); 
        $j('#tabs>div').hide();
        $j(currentTab).show();
        return false;
    });


    if ($j('#shiping_prices').get(0)){
        $j('#shiping_prices').change(function() {
            result = $j(this).val()
            if (result.indexOf('---') != -1) {
                var amount = result.split('---',2);
                var amountNumeric = parseFloat(amount[1].replace(/[^0-9\.]/g, ''))
                var totalNumeric = $j('#subtotal').html()
                totalNumeric = parseFloat(totalNumeric.replace(/[^0-9,\.]/g, '')) + amountNumeric
                $j('#shipPrice').html(amount[1])
                $j('#cartTotal').html("$" + totalNumeric)
            } else{
                $j('#cartTotal').html('---')
                $j('#shipPrice').html()
            }
        });
    }


    $j(function(){

        $j('form.f_top .inp').keydown(function(){
            var input = $j('.inp input').val();
            if ((input.length == 1) && ($j('.close').length == 0)) {
                $j(this).append($j('<div class="close"></div>'));
            } else if (input.length == 0) {
                $j('.close').remove();
            }
        });

        $j('form.f_top .inp').keyup(function(){
            var input = $j('.inp input').val();
            if ((input.length == 1) && ($j('.close').length == 0)) {
                $j(this).append($j('<div class="close"></div>'));
            } else if (input.length == 0) {
                $j('.close').remove();
            }
        });

        $j('form.f_top .close').live('click', function(){
            $j('.inp input').val('find it here..');
            $j(this).remove();
        });

        maxHeight = 220;
        minHeight = 17;
        $j('#rc .good_know .fold').mouseenter(
            function(){
                $j(this).siblings().stop().animate({
                    height: minHeight+"px"
                }, {
                    queue:false,
                    duration:300
                }, function(){

                }).find('a.title').removeClass('active');
                $j(this).stop().animate({
                    height: maxHeight+"px"
                }, {
                    queue:false,
                    duration:300
                }).find('a.title').addClass('active');
            });
        $j('#rc .good_know .fold:first').trigger('mouseenter');
        //$j('#rc .good_know .fold:first').animate({height: maxWidth+"px"}, { queue:false, duration:400 });

        //slider script starts here

        var previous_id = 1;
        $j('#slider').jCarouselLite({
            visible: 1,
            speed: 800,
            circular: true,
            auto:60000,
            btnGo: ["#sliderButton1", "#sliderButton2", "#sliderButton3"],
            beforeStart: function(li) {
                if(previous_id)
                    $j('#sliderButton'+previous_id).css('background-position', 'left top');
            },
            afterEnd: function(li) {
                var id = li.children('a').attr('href').substr(1,2);
                $j('#sliderButton'+id).css('background-position', 'right top');
                previous_id = id;
            },
            pauseOnHover: true
        });

        maxWidth = 87;
        minWidth = 0;
        $j("ul.pfinder li").mouseenter(
            function(){
                $j(this).siblings().animate({
                    width: minWidth+"px"
                }, {
                    queue:false,
                    duration:400
                });
                $j(this).siblings().children('div.in').animate({
                    width: minWidth+"px"
                }, {
                    queue:false,
                    duration:400
                });
                $j(this).animate({
                    width: maxWidth+"px"
                }, {
                    queue:false,
                    duration:400
                });
                $j(this).children('div.in').animate({
                    width: maxWidth+"px"
                }, {
                    queue:false,
                    duration:400
                });
            });

    
        $j('#mycarousel').jCarouselLite({
            btnNext: ".jcarousel-next",
            btnPrev: ".jcarousel-prev",
            circular: false,
            vertical: true,
            scroll: 1,
            visible: 2,
            mouseWheel: true
        });
			
			/* Unlocking form validate */
			
			$j('#cart_quantity').validate();
			
			/* Hide tool tips to start */
			
			$j(".tool_tip").hide();
			
			/* Behavoir of each area */
			
			// Unlocking action area
			
			$j("#give_hints").mouseover(function() {

				$j(".tool_tip").show("slow");

			});
			
			
			// Modals
			
			$j('#service_guarantee_modal').jqm();
		

        if($j('#summary_box').get(0)){
            $j(window).scroll(function() {
                var box = $j('#summary_box').get(0);
                var boxh = box.offsetHeight
                var h = $j(this).scrollTop()+boxh
                var limit = $j('#lc').get(0).offsetHeight
                if (h>limit && $j(box).css('position')=='fixed'){
                    $j(box).css('position','absolute').css('marginTop',$j(box).position().top+boxh)
                    $j(box).animate({
                        marginTop:limit-box.offsetHeight
                    })
                }
                if (h<=limit && $j(box).css('position')=='absolute'){
                    $j(box).css('marginTop',0).css('position','fixed')
                }
            });
        }


    });
});


function goforward()
{
    var manufac_id = document.getElementById('manu_id').value;
    var modelid = document.getElementById('model_id').value;
    window.location.href="product_model_info.php?&model_id="+modelid;
}
